home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / expmed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-25  |  94.4 KB  |  3,057 lines  |  [TEXT/MPS ]

  1. /* Medium-level subroutines: convert bit-field store and extract
  2.    and shifts, multiplies and divides to rtl instructions.
  3.    Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "config.h"
  22. #include "rtl.h"
  23. #include "tree.h"
  24. #include "flags.h"
  25. #include "insn-flags.h"
  26. #include "insn-codes.h"
  27. #include "insn-config.h"
  28. #include "expr.h"
  29. #include "real.h"
  30. #include "recog.h"
  31.  
  32. static rtx extract_split_bit_field ();
  33. static rtx extract_fixed_bit_field ();
  34. static void store_split_bit_field ();
  35. static void store_fixed_bit_field ();
  36. static rtx mask_rtx ();
  37. static rtx lshift_value ();
  38.  
  39. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  40.  
  41. /* Non-zero means multiply instructions are cheaper than shifts.  */
  42. int mult_is_very_cheap;
  43.  
  44. /* Non-zero means divides or modulus operations are relatively cheap for
  45.    powers of two, so don't use branches; emit the operation instead. 
  46.    Usually, this will mean that the MD file will emit non-branch
  47.    sequences.  */
  48.  
  49. static int sdiv_pow2_cheap, smod_pow2_cheap;
  50.  
  51. /* Cost of various pieces of RTL.  */
  52. static int add_cost, shift_cost, mult_cost, negate_cost, lea_cost;
  53.  
  54. /* Max scale factor for scaled address in lea instruction.  */
  55. static int lea_max_mul;
  56.  
  57. void
  58. init_expmed ()
  59. {
  60.   char *free_point = (char *) oballoc (1);
  61.   /* This is "some random pseudo register" for purposes of calling recog
  62.      to see what insns exist.  */
  63.   rtx reg = gen_rtx (REG, word_mode, FIRST_PSEUDO_REGISTER);
  64.   rtx pow2 = GEN_INT (32);
  65.   rtx lea;
  66.   HOST_WIDE_INT i;
  67.   int dummy;
  68.  
  69.   add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
  70.   shift_cost = rtx_cost (gen_rtx (LSHIFT, word_mode, reg,
  71.                   /* Using a constant gives better
  72.                      estimate of typical costs.
  73.                      1 or 2 might have quirks.  */
  74.                   GEN_INT (3)), SET);
  75.   mult_cost = rtx_cost (gen_rtx (MULT, word_mode, reg, reg), SET);
  76.   negate_cost = rtx_cost (gen_rtx (NEG, word_mode, reg), SET);
  77.  
  78.   /* 999999 is chosen to avoid any plausible faster special case.  */
  79.   mult_is_very_cheap
  80.     = (rtx_cost (gen_rtx (MULT, word_mode, reg, GEN_INT (999999)), SET)
  81.        < rtx_cost (gen_rtx (LSHIFT, word_mode, reg, GEN_INT (7)), SET));
  82.  
  83.   sdiv_pow2_cheap
  84.     = rtx_cost (gen_rtx (DIV, word_mode, reg, pow2), SET) <= 2 * add_cost;
  85.   smod_pow2_cheap
  86.     = rtx_cost (gen_rtx (MOD, word_mode, reg, pow2), SET) <= 2 * add_cost;
  87.  
  88.   init_recog ();
  89.   for (i = 2;; i <<= 1)
  90.     {
  91.       lea = gen_rtx (SET, VOIDmode, reg,
  92.              gen_rtx (PLUS, word_mode,
  93.                   gen_rtx (MULT, word_mode, reg, GEN_INT (i)),
  94.                   reg));
  95.       /* Using 0 as second argument is not quite right,
  96.      but what else is there to do?  */
  97.       if (recog (lea, 0, &dummy) < 0)
  98.     break;
  99.       lea_max_mul = i;
  100.       lea_cost = rtx_cost (SET_SRC (lea), SET);
  101.     }
  102.  
  103.   /* Free the objects we just allocated.  */
  104.   obfree (free_point);
  105. }
  106.  
  107. /* Return an rtx representing minus the value of X.
  108.    MODE is the intended mode of the result,
  109.    useful if X is a CONST_INT.  */
  110.  
  111. rtx
  112. negate_rtx (mode, x)
  113.      enum machine_mode mode;
  114.      rtx x;
  115. {
  116.   if (GET_CODE (x) == CONST_INT)
  117.     {
  118.       HOST_WIDE_INT val = - INTVAL (x);
  119.       if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
  120.     {
  121.       /* Sign extend the value from the bits that are significant.  */
  122.       if (val & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
  123.         val |= (HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (mode);
  124.       else
  125.         val &= ((HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (mode)) - 1;
  126.     }
  127.       return GEN_INT (val);
  128.     }
  129.   else
  130.     return expand_unop (GET_MODE (x), neg_optab, x, NULL_RTX, 0);
  131. }
  132.  
  133. /* Generate code to store value from rtx VALUE
  134.    into a bit-field within structure STR_RTX
  135.    containing BITSIZE bits starting at bit BITNUM.
  136.    FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
  137.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  138.    TOTAL_SIZE is the size of the structure in bytes, or -1 if varying.  */
  139.  
  140. /* ??? Note that there are two different ideas here for how
  141.    to determine the size to count bits within, for a register.
  142.    One is BITS_PER_WORD, and the other is the size of operand 3
  143.    of the insv pattern.  (The latter assumes that an n-bit machine
  144.    will be able to insert bit fields up to n bits wide.)
  145.    It isn't certain that either of these is right.
  146.    extract_bit_field has the same quandary.  */
  147.  
  148. rtx
  149. store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
  150.      rtx str_rtx;
  151.      register int bitsize;
  152.      int bitnum;
  153.      enum machine_mode fieldmode;
  154.      rtx value;
  155.      int align;
  156.      int total_size;
  157. {
  158.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  159.   register int offset = bitnum / unit;
  160.   register int bitpos = bitnum % unit;
  161.   register rtx op0 = str_rtx;
  162.  
  163.   if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
  164.     abort ();
  165.  
  166.   /* Discount the part of the structure before the desired byte.
  167.      We need to know how many bytes are safe to reference after it.  */
  168.   if (total_size >= 0)
  169.     total_size -= (bitpos / BIGGEST_ALIGNMENT
  170.            * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  171.  
  172.   while (GET_CODE (op0) == SUBREG)
  173.     {
  174.       /* The following line once was done only if WORDS_BIG_ENDIAN,
  175.      but I think that is a mistake.  WORDS_BIG_ENDIAN is
  176.      meaningful at a much higher level; when structures are copied
  177.      between memory and regs, the higher-numbered regs
  178.      always get higher addresses.  */
  179.       offset += SUBREG_WORD (op0);
  180.       /* We used to adjust BITPOS here, but now we do the whole adjustment
  181.      right after the loop.  */
  182.       op0 = SUBREG_REG (op0);
  183.     }
  184.  
  185. #if BYTES_BIG_ENDIAN
  186.   /* If OP0 is a register, BITPOS must count within a word.
  187.      But as we have it, it counts within whatever size OP0 now has.
  188.      On a bigendian machine, these are not the same, so convert.  */
  189.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  190.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  191. #endif
  192.  
  193.   value = protect_from_queue (value, 0);
  194.  
  195.   if (flag_force_mem)
  196.     value = force_not_mem (value);
  197.  
  198.   /* Note that the adjustment of BITPOS above has no effect on whether
  199.      BITPOS is 0 in a REG bigger than a word.  */
  200.   if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
  201.       && (! STRICT_ALIGNMENT || GET_CODE (op0) != MEM)
  202.       && bitpos == 0 && bitsize == GET_MODE_BITSIZE (fieldmode))
  203.     {
  204.       /* Storing in a full-word or multi-word field in a register
  205.      can be done with just SUBREG.  */
  206.       if (GET_MODE (op0) != fieldmode)
  207.     if (GET_CODE (op0) == REG)
  208.       op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
  209.     else
  210.       op0 = change_address (op0, fieldmode,
  211.                 plus_constant (XEXP (op0, 0), offset));
  212.       emit_move_insn (op0, value);
  213.       return value;
  214.     }
  215.  
  216.   /* Storing an lsb-aligned field in a register
  217.      can be done with a movestrict instruction.  */
  218.  
  219.   if (GET_CODE (op0) != MEM
  220. #if BYTES_BIG_ENDIAN
  221.       && bitpos + bitsize == unit
  222. #else
  223.       && bitpos == 0
  224. #endif
  225.       && bitsize == GET_MODE_BITSIZE (fieldmode)
  226.       && (GET_MODE (op0) == fieldmode
  227.       || (movstrict_optab->handlers[(int) fieldmode].insn_code
  228.           != CODE_FOR_nothing)))
  229.     {
  230.       /* Get appropriate low part of the value being stored.  */
  231.       if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
  232.     value = gen_lowpart (fieldmode, value);
  233.       else if (!(GET_CODE (value) == SYMBOL_REF
  234.          || GET_CODE (value) == LABEL_REF
  235.          || GET_CODE (value) == CONST))
  236.     value = convert_to_mode (fieldmode, value, 0);
  237.  
  238.       if (GET_MODE (op0) == fieldmode)
  239.     emit_move_insn (op0, value);
  240.       else
  241.     {
  242.       int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
  243.       if(! (*insn_operand_predicate[icode][1]) (value, fieldmode))
  244.         value = copy_to_mode_reg (fieldmode, value);
  245.       emit_insn (GEN_FCN (icode)
  246.            (gen_rtx (SUBREG, fieldmode, op0, offset), value));
  247.     }
  248.       return value;
  249.     }
  250.  
  251.   /* Handle fields bigger than a word.  */
  252.  
  253.   if (bitsize > BITS_PER_WORD)
  254.     {
  255.       /* Here we transfer the words of the field
  256.      in the order least significant first.
  257.      This is because the most significant word is the one which may
  258.      be less than full.  */
  259.  
  260.       int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
  261.       int i;
  262.  
  263.       /* This is the mode we must force value to, so that there will be enough
  264.      subwords to extract.  Note that fieldmode will often (always?) be
  265.      VOIDmode, because that is what store_field uses to indicate that this
  266.      is a bit field, but passing VOIDmode to operand_subword_force will
  267.      result in an abort.  */
  268.       fieldmode = mode_for_size (nwords * BITS_PER_WORD, MODE_INT, 0);
  269.  
  270.       for (i = 0; i < nwords; i++)
  271.     {
  272.       /* If I is 0, use the low-order word in both field and target;
  273.          if I is 1, use the next to lowest word; and so on.  */
  274.       int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
  275.       int bit_offset = (WORDS_BIG_ENDIAN
  276.                 ? MAX (bitsize - (i + 1) * BITS_PER_WORD, 0)
  277.                 : i * BITS_PER_WORD);
  278.       store_bit_field (op0, MIN (BITS_PER_WORD,
  279.                      bitsize - i * BITS_PER_WORD),
  280.                bitnum + bit_offset, word_mode,
  281.                operand_subword_force (value, wordnum, fieldmode),
  282.                align, total_size);
  283.     }
  284.       return value;
  285.     }
  286.  
  287.   /* From here on we can assume that the field to be stored in is
  288.      a full-word (whatever type that is), since it is shorter than a word.  */
  289.  
  290.   /* OFFSET is the number of words or bytes (UNIT says which)
  291.      from STR_RTX to the first word or byte containing part of the field.  */
  292.  
  293.   if (GET_CODE (op0) == REG)
  294.     {
  295.       if (offset != 0
  296.       || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
  297.     op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
  298.                op0, offset);
  299.       offset = 0;
  300.     }
  301.   else
  302.     {
  303.       op0 = protect_from_queue (op0, 1);
  304.     }
  305.  
  306.   /* Now OFFSET is nonzero only if OP0 is memory
  307.      and is therefore always measured in bytes.  */
  308.  
  309. #ifdef HAVE_insv
  310.   if (HAVE_insv
  311.       && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
  312.       /* Ensure insv's size is wide enough for this field.  */
  313.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3])
  314.       >= bitsize))
  315.     {
  316.       int xbitpos = bitpos;
  317.       rtx value1;
  318.       rtx xop0 = op0;
  319.       rtx last = get_last_insn ();
  320.       rtx pat;
  321.       enum machine_mode maxmode
  322.     = insn_operand_mode[(int) CODE_FOR_insv][3];
  323.  
  324.       int save_volatile_ok = volatile_ok;
  325.       volatile_ok = 1;
  326.  
  327.       /* If this machine's insv can only insert into a register, or if we
  328.      are to force MEMs into a register, copy OP0 into a register and
  329.      save it back later.  */
  330.       if (GET_CODE (op0) == MEM
  331.       && (flag_force_mem
  332.           || ! ((*insn_operand_predicate[(int) CODE_FOR_insv][0])
  333.             (op0, VOIDmode))))
  334.     {
  335.       rtx tempreg;
  336.       enum machine_mode bestmode;
  337.  
  338.       /* Get the mode to use for inserting into this field.  If OP0 is
  339.          BLKmode, get the smallest mode consistent with the alignment. If
  340.          OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
  341.          mode. Otherwise, use the smallest mode containing the field.  */
  342.  
  343.       if (GET_MODE (op0) == BLKmode
  344.           || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
  345.         bestmode
  346.           = get_best_mode (bitsize, bitnum, align * BITS_PER_UNIT, maxmode,
  347.                    MEM_VOLATILE_P (op0));
  348.       else
  349.         bestmode = GET_MODE (op0);
  350.  
  351.       if (bestmode == VOIDmode)
  352.         goto insv_loses;
  353.  
  354.       /* Adjust address to point to the containing unit of that mode.  */
  355.       unit = GET_MODE_BITSIZE (bestmode);
  356.       /* Compute offset as multiple of this unit, counting in bytes.  */
  357.       offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  358.       bitpos = bitnum % unit;
  359.       op0 = change_address (op0, bestmode, 
  360.                 plus_constant (XEXP (op0, 0), offset));
  361.  
  362.       /* Fetch that unit, store the bitfield in it, then store the unit.  */
  363.       tempreg = copy_to_reg (op0);
  364.       store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
  365.                align, total_size);
  366.       emit_move_insn (op0, tempreg);
  367.       return value;
  368.     }
  369.       volatile_ok = save_volatile_ok;
  370.  
  371.       /* Add OFFSET into OP0's address.  */
  372.       if (GET_CODE (xop0) == MEM)
  373.     xop0 = change_address (xop0, byte_mode,
  374.                    plus_constant (XEXP (xop0, 0), offset));
  375.  
  376.       /* If xop0 is a register, we need it in MAXMODE
  377.      to make it acceptable to the format of insv.  */
  378.       if (GET_CODE (xop0) == SUBREG)
  379.     PUT_MODE (xop0, maxmode);
  380.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  381.     xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  382.  
  383.       /* On big-endian machines, we count bits from the most significant.
  384.      If the bit field insn does not, we must invert.  */
  385.  
  386. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  387.       xbitpos = unit - bitsize - xbitpos;
  388. #endif
  389.       /* We have been counting XBITPOS within UNIT.
  390.      Count instead within the size of the register.  */
  391. #if BITS_BIG_ENDIAN
  392.       if (GET_CODE (xop0) != MEM)
  393.     xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
  394. #endif
  395.       unit = GET_MODE_BITSIZE (maxmode);
  396.  
  397.       /* Convert VALUE to maxmode (which insv insn wants) in VALUE1.  */
  398.       value1 = value;
  399.       if (GET_MODE (value) != maxmode)
  400.     {
  401.       if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  402.         {
  403.           /* Optimization: Don't bother really extending VALUE
  404.          if it has all the bits we will actually use.  However,
  405.          if we must narrow it, be sure we do it correctly.  */
  406.  
  407.           if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
  408.         {
  409.           /* Avoid making subreg of a subreg, or of a mem.  */
  410.           if (GET_CODE (value1) != REG)
  411.         value1 = copy_to_reg (value1);
  412.           value1 = gen_rtx (SUBREG, maxmode, value1, 0);
  413.         }
  414.           else
  415.         value1 = gen_lowpart (maxmode, value1);
  416.         }
  417.       else if (!CONSTANT_P (value))
  418.         /* Parse phase is supposed to make VALUE's data type
  419.            match that of the component reference, which is a type
  420.            at least as wide as the field; so VALUE should have
  421.            a mode that corresponds to that type.  */
  422.         abort ();
  423.     }
  424.  
  425.       /* If this machine's insv insists on a register,
  426.      get VALUE1 into a register.  */
  427.       if (! ((*insn_operand_predicate[(int) CODE_FOR_insv][3])
  428.          (value1, maxmode)))
  429.     value1 = force_reg (maxmode, value1);
  430.  
  431.       pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
  432.       if (pat)
  433.     emit_insn (pat);
  434.       else
  435.         {
  436.       delete_insns_since (last);
  437.       store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  438.     }
  439.     }
  440.   else
  441.     insv_loses:
  442. #endif
  443.     /* Insv is not available; store using shifts and boolean ops.  */
  444.     store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  445.   return value;
  446. }
  447.  
  448. /* Use shifts and boolean operations to store VALUE
  449.    into a bit field of width BITSIZE
  450.    in a memory location specified by OP0 except offset by OFFSET bytes.
  451.      (OFFSET must be 0 if OP0 is a register.)
  452.    The field starts at position BITPOS within the byte.
  453.     (If OP0 is a register, it may be a full word or a narrower mode,
  454.      but BITPOS still counts within a full word,
  455.      which is significant on bigendian machines.)
  456.    STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
  457.  
  458.    Note that protect_from_queue has already been done on OP0 and VALUE.  */
  459.  
  460. static void
  461. store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
  462.      register rtx op0;
  463.      register int offset, bitsize, bitpos;
  464.      register rtx value;
  465.      int struct_align;
  466. {
  467.   register enum machine_mode mode;
  468.   int total_bits = BITS_PER_WORD;
  469.   rtx subtarget, temp;
  470.   int all_zero = 0;
  471.   int all_one = 0;
  472.  
  473.   /* Add OFFSET to OP0's address (if it is in memory)
  474.      and if a single byte contains the whole bit field
  475.      change OP0 to a byte.  */
  476.  
  477.   /* There is a case not handled here:
  478.      a structure with a known alignment of just a halfword
  479.      and a field split across two aligned halfwords within the structure.
  480.      Or likewise a structure with a known alignment of just a byte
  481.      and a field split across two bytes.
  482.      Such cases are not supposed to be able to occur.  */
  483.  
  484.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  485.     {
  486.       if (offset != 0)
  487.     abort ();
  488.       /* Special treatment for a bit field split across two registers.  */
  489.       if (bitsize + bitpos > BITS_PER_WORD)
  490.     {
  491.       store_split_bit_field (op0, bitsize, bitpos, value, BITS_PER_WORD);
  492.       return;
  493.     }
  494.     }
  495.   else
  496.     {
  497.       /* Get the proper mode to use for this field.  We want a mode that
  498.      includes the entire field.  If such a mode would be larger than
  499.      a word, we won't be doing the extraction the normal way.  */
  500.  
  501.       mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
  502.                 struct_align * BITS_PER_UNIT, word_mode,
  503.                 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
  504.  
  505.       if (mode == VOIDmode)
  506.     {
  507.       /* The only way this should occur is if the field spans word
  508.          boundaries.  */
  509.       store_split_bit_field (op0, bitsize, bitpos + offset * BITS_PER_UNIT,
  510.                  value, struct_align);
  511.       return;
  512.     }
  513.  
  514.       total_bits = GET_MODE_BITSIZE (mode);
  515.  
  516.       /* Get ref to an aligned byte, halfword, or word containing the field.
  517.      Adjust BITPOS to be position within a word,
  518.      and OFFSET to be the offset of that word.
  519.      Then alter OP0 to refer to that word.  */
  520.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  521.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  522.       op0 = change_address (op0, mode,
  523.                 plus_constant (XEXP (op0, 0), offset));
  524.     }
  525.  
  526.   mode = GET_MODE (op0);
  527.  
  528.   /* Now MODE is either some integral mode for a MEM as OP0,
  529.      or is a full-word for a REG as OP0.  TOTAL_BITS corresponds.
  530.      The bit field is contained entirely within OP0.
  531.      BITPOS is the starting bit number within OP0.
  532.      (OP0's mode may actually be narrower than MODE.)  */
  533.  
  534. #if BYTES_BIG_ENDIAN
  535.   /* BITPOS is the distance between our msb
  536.      and that of the containing datum.
  537.      Convert it to the distance from the lsb.  */
  538.  
  539.   bitpos = total_bits - bitsize - bitpos;
  540. #endif
  541.   /* Now BITPOS is always the distance between our lsb
  542.      and that of OP0.  */
  543.  
  544.   /* Shift VALUE left by BITPOS bits.  If VALUE is not constant,
  545.      we must first convert its mode to MODE.  */
  546.  
  547.   if (GET_CODE (value) == CONST_INT)
  548.     {
  549.       register HOST_WIDE_INT v = INTVAL (value);
  550.  
  551.       if (bitsize < HOST_BITS_PER_WIDE_INT)
  552.     v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
  553.  
  554.       if (v == 0)
  555.     all_zero = 1;
  556.       else if ((bitsize < HOST_BITS_PER_WIDE_INT
  557.         && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
  558.            || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
  559.     all_one = 1;
  560.  
  561.       value = lshift_value (mode, value, bitpos, bitsize);
  562.     }
  563.   else
  564.     {
  565.       int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
  566.               && bitpos + bitsize != GET_MODE_BITSIZE (mode));
  567.  
  568.       if (GET_MODE (value) != mode)
  569.     {
  570.       /* If VALUE is a floating-point mode, access it as an integer
  571.          of the corresponding size, then convert it.  This can occur on
  572.          a machine with 64 bit registers that uses SFmode for float.  */
  573.       if (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT)
  574.         {
  575.           if (GET_CODE (value) != REG)
  576.         value = copy_to_reg (value);
  577.           value
  578.         = gen_rtx (SUBREG, word_mode, value, 0);
  579.         }
  580.  
  581.       if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
  582.           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
  583.         value = gen_lowpart (mode, value);
  584.       else
  585.         value = convert_to_mode (mode, value, 1);
  586.     }
  587.  
  588.       if (must_and)
  589.     value = expand_binop (mode, and_optab, value,
  590.                   mask_rtx (mode, 0, bitsize, 0),
  591.                   NULL_RTX, 1, OPTAB_LIB_WIDEN);
  592.       if (bitpos > 0)
  593.     value = expand_shift (LSHIFT_EXPR, mode, value,
  594.                   build_int_2 (bitpos, 0), NULL_RTX, 1);
  595.     }
  596.  
  597.   /* Now clear the chosen bits in OP0,
  598.      except that if VALUE is -1 we need not bother.  */
  599.  
  600.   subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
  601.  
  602.   if (! all_one)
  603.     {
  604.       temp = expand_binop (mode, and_optab, op0,
  605.                mask_rtx (mode, bitpos, bitsize, 1),
  606.                subtarget, 1, OPTAB_LIB_WIDEN);
  607.       subtarget = temp;
  608.     }
  609.   else
  610.     temp = op0;
  611.  
  612.   /* Now logical-or VALUE into OP0, unless it is zero.  */
  613.  
  614.   if (! all_zero)
  615.     temp = expand_binop (mode, ior_optab, temp, value,
  616.              subtarget, 1, OPTAB_LIB_WIDEN);
  617.   if (op0 != temp)
  618.     emit_move_insn (op0, temp);
  619. }
  620.  
  621. /* Store a bit field that is split across two words.
  622.  
  623.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  624.    BITSIZE is the field width; BITPOS the position of its first bit
  625.    (within the word).
  626.    VALUE is the value to store.  */
  627.  
  628. static void
  629. store_split_bit_field (op0, bitsize, bitpos, value, align)
  630.      rtx op0;
  631.      int bitsize, bitpos;
  632.      rtx value;
  633.      int align;
  634. {
  635.   /* BITSIZE_1 is size of the part in the first word.  */
  636.   int bitsize_1 = BITS_PER_WORD - bitpos % BITS_PER_WORD;
  637.   /* BITSIZE_2 is size of the rest (in the following word).  */
  638.   int bitsize_2 = bitsize - bitsize_1;
  639.   rtx part1, part2;
  640.   int unit = GET_CODE (op0) == MEM ? BITS_PER_UNIT : BITS_PER_WORD;
  641.   int offset = bitpos / unit;
  642.   rtx word;
  643.  
  644. #if 0
  645. /*This bug shows up when NOT compiling for 68020*/
  646. fprintf(stderr, "op0 = "); debug_rtx(op0);
  647. fprintf(stderr, "bitsize = %d\n", bitsize);
  648. fprintf(stderr, "bitpos = %d\n", bitpos);
  649. fprintf(stderr, "value = "); debug_rtx(value);
  650. fprintf(stderr, "align = %d\n", align);
  651. fprintf(stderr, "BITS_PER_WORD = %d\n", BITS_PER_WORD);
  652. #endif
  653.  
  654.   /* The field must span exactly one word boundary.  */
  655.   if (bitpos / BITS_PER_WORD != (bitpos + bitsize - 1) / BITS_PER_WORD - 1)
  656.     abort ();
  657.  
  658.   if (GET_MODE (value) != VOIDmode)
  659.     value = convert_to_mode (word_mode, value, 1);
  660.   if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
  661.     value = copy_to_reg (value);
  662.  
  663.   /* Split the value into two parts:
  664.      PART1 gets that which goes in the first word; PART2 the other.  */
  665. #if BYTES_BIG_ENDIAN
  666.   /* PART1 gets the more significant part.  */
  667.   if (GET_CODE (value) == CONST_INT)
  668.     {
  669.       part1 = GEN_INT ((unsigned HOST_WIDE_INT) (INTVAL (value)) >> bitsize_2);
  670.       part2 = GEN_INT ((unsigned HOST_WIDE_INT) (INTVAL (value))
  671.                & (((HOST_WIDE_INT) 1 << bitsize_2) - 1));
  672.     }
  673.   else
  674.     {
  675.       part1 = extract_fixed_bit_field (word_mode, value, 0, bitsize_1,
  676.                        BITS_PER_WORD - bitsize, NULL_RTX, 1,
  677.                        BITS_PER_WORD);
  678.       part2 = extract_fixed_bit_field (word_mode, value, 0, bitsize_2,
  679.                        BITS_PER_WORD - bitsize_2, NULL_RTX, 1,
  680.                        BITS_PER_WORD);
  681.     }
  682. #else
  683.   /* PART1 gets the less significant part.  */
  684.   if (GET_CODE (value) == CONST_INT)
  685.     {
  686.       part1 = GEN_INT ((unsigned HOST_WIDE_INT) (INTVAL (value))
  687.                & (((HOST_WIDE_INT) 1 << bitsize_1) - 1));
  688.       part2 = GEN_INT ((unsigned HOST_WIDE_INT) (INTVAL (value)) >> bitsize_1);
  689.     }
  690.   else
  691.     {
  692.       part1 = extract_fixed_bit_field (word_mode, value, 0, bitsize_1, 0,
  693.                        NULL_RTX, 1, BITS_PER_WORD);
  694.       part2 = extract_fixed_bit_field (word_mode, value, 0, bitsize_2,
  695.                        bitsize_1, NULL_RTX, 1, BITS_PER_WORD);
  696.     }
  697. #endif
  698.  
  699.   /* Store PART1 into the first word.  If OP0 is a MEM, pass OP0 and the
  700.      offset computed above.  Otherwise, get the proper word and pass an
  701.      offset of zero.  */
  702.   word = (GET_CODE (op0) == MEM ? op0
  703.       : operand_subword (op0, offset, 1, GET_MODE (op0)));
  704.   if (word == 0)
  705.     abort ();
  706.  
  707.   store_fixed_bit_field (word, GET_CODE (op0) == MEM ? offset : 0,
  708.              bitsize_1, bitpos % unit, part1, align);
  709.  
  710.   /* Offset op0 by 1 word to get to the following one.  */
  711.   if (GET_CODE (op0) == SUBREG)
  712.     word = operand_subword (SUBREG_REG (op0), SUBREG_WORD (op0) + offset + 1,
  713.                 1, VOIDmode);
  714.   else if (GET_CODE (op0) == MEM)
  715.     word = op0;
  716.   else
  717.     word = operand_subword (op0, offset + 1, 1, GET_MODE (op0));
  718.  
  719.   if (word == 0)
  720.     abort ();
  721.  
  722.   /* Store PART2 into the second word.  */
  723.   store_fixed_bit_field (word,
  724.              (GET_CODE (op0) == MEM
  725.               ? CEIL (offset + 1, UNITS_PER_WORD) * UNITS_PER_WORD
  726.               : 0),
  727.              bitsize_2, 0, part2, align);
  728. }
  729.  
  730. /* Generate code to extract a byte-field from STR_RTX
  731.    containing BITSIZE bits, starting at BITNUM,
  732.    and put it in TARGET if possible (if TARGET is nonzero).
  733.    Regardless of TARGET, we return the rtx for where the value is placed.
  734.    It may be a QUEUED.
  735.  
  736.    STR_RTX is the structure containing the byte (a REG or MEM).
  737.    UNSIGNEDP is nonzero if this is an unsigned bit field.
  738.    MODE is the natural mode of the field value once extracted.
  739.    TMODE is the mode the caller would like the value to have;
  740.    but the value may be returned with type MODE instead.
  741.  
  742.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  743.    TOTAL_SIZE is the size in bytes of the containing structure,
  744.    or -1 if varying.
  745.  
  746.    If a TARGET is specified and we can store in it at no extra cost,
  747.    we do so, and return TARGET.
  748.    Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  749.    if they are equally easy.  */
  750.  
  751. rtx
  752. extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
  753.            target, mode, tmode, align, total_size)
  754.      rtx str_rtx;
  755.      register int bitsize;
  756.      int bitnum;
  757.      int unsignedp;
  758.      rtx target;
  759.      enum machine_mode mode, tmode;
  760.      int align;
  761.      int total_size;
  762. {
  763.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  764.   register int offset = bitnum / unit;
  765.   register int bitpos = bitnum % unit;
  766.   register rtx op0 = str_rtx;
  767.   rtx spec_target = target;
  768.   rtx spec_target_subreg = 0;
  769.  
  770.   if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
  771.     abort ();
  772.  
  773.   /* Discount the part of the structure before the desired byte.
  774.      We need to know how many bytes are safe to reference after it.  */
  775.   if (total_size >= 0)
  776.     total_size -= (bitpos / BIGGEST_ALIGNMENT
  777.            * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  778.  
  779.   if (tmode == VOIDmode)
  780.     tmode = mode;
  781.   while (GET_CODE (op0) == SUBREG)
  782.     {
  783.       offset += SUBREG_WORD (op0);
  784.       op0 = SUBREG_REG (op0);
  785.     }
  786.   
  787. #if BYTES_BIG_ENDIAN
  788.   /* If OP0 is a register, BITPOS must count within a word.
  789.      But as we have it, it counts within whatever size OP0 now has.
  790.      On a bigendian machine, these are not the same, so convert.  */
  791.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  792.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  793. #endif
  794.  
  795.   /* Extracting a full-word or multi-word value
  796.      from a structure in a register.
  797.      This can be done with just SUBREG.
  798.      So too extracting a subword value in
  799.      the least significant part of the register.  */
  800.  
  801.   if (GET_CODE (op0) == REG
  802.       && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
  803.        && bitpos % BITS_PER_WORD == 0)
  804.       || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
  805. #if BYTES_BIG_ENDIAN
  806.           && bitpos + bitsize == BITS_PER_WORD
  807. #else
  808.           && bitpos == 0
  809. #endif
  810.           )))
  811.     {
  812.       enum machine_mode mode1
  813.     = mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0);
  814.  
  815.       if (mode1 != GET_MODE (op0))
  816.     op0 = gen_rtx (SUBREG, mode1, op0, offset);
  817.  
  818.       if (mode1 != mode)
  819.     return convert_to_mode (tmode, op0, unsignedp);
  820.       return op0;
  821.     }
  822.  
  823.   /* Handle fields bigger than a word.  */
  824.   
  825.   if (bitsize > BITS_PER_WORD)
  826.     {
  827.       /* Here we transfer the words of the field
  828.      in the order least significant first.
  829.      This is because the most significant word is the one which may
  830.      be less than full.  */
  831.  
  832.       int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
  833.       int i;
  834.  
  835.       if (target == 0 || GET_CODE (target) != REG)
  836.     target = gen_reg_rtx (mode);
  837.  
  838.       for (i = 0; i < nwords; i++)
  839.     {
  840.       /* If I is 0, use the low-order word in both field and target;
  841.          if I is 1, use the next to lowest word; and so on.  */
  842.       int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
  843.       int bit_offset = (WORDS_BIG_ENDIAN
  844.                 ? MAX (0, bitsize - (i + 1) * BITS_PER_WORD)
  845.                 : i * BITS_PER_WORD);
  846.       rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
  847.       rtx result_part
  848.         = extract_bit_field (op0, MIN (BITS_PER_WORD,
  849.                        bitsize - i * BITS_PER_WORD),
  850.                  bitnum + bit_offset,
  851.                  1, target_part, mode, word_mode,
  852.                  align, total_size);
  853.  
  854.       if (target_part == 0)
  855.         abort ();
  856.  
  857.       if (result_part != target_part)
  858.         emit_move_insn (target_part, result_part);
  859.     }
  860.  
  861.       return target;
  862.     }
  863.   
  864.   /* From here on we know the desired field is smaller than a word
  865.      so we can assume it is an integer.  So we can safely extract it as one
  866.      size of integer, if necessary, and then truncate or extend
  867.      to the size that is wanted.  */
  868.  
  869.   /* OFFSET is the number of words or bytes (UNIT says which)
  870.      from STR_RTX to the first word or byte containing part of the field.  */
  871.  
  872.   if (GET_CODE (op0) == REG)
  873.     {
  874.       if (offset != 0
  875.       || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
  876.     op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
  877.                op0, offset);
  878.       offset = 0;
  879.     }
  880.   else
  881.     {
  882.       op0 = protect_from_queue (str_rtx, 1);
  883.     }
  884.  
  885.   /* Now OFFSET is nonzero only for memory operands.  */
  886.  
  887.   if (unsignedp)
  888.     {
  889. #ifdef HAVE_extzv
  890.       if (HAVE_extzv
  891.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0])
  892.           >= bitsize))
  893.     {
  894.       int xbitpos = bitpos, xoffset = offset;
  895.       rtx bitsize_rtx, bitpos_rtx;
  896.       rtx last = get_last_insn();
  897.       rtx xop0 = op0;
  898.       rtx xtarget = target;
  899.       rtx xspec_target = spec_target;
  900.       rtx xspec_target_subreg = spec_target_subreg;
  901.       rtx pat;
  902.       enum machine_mode maxmode
  903.         = insn_operand_mode[(int) CODE_FOR_extzv][0];
  904.  
  905.       if (GET_CODE (xop0) == MEM)
  906.         {
  907.           int save_volatile_ok = volatile_ok;
  908.           volatile_ok = 1;
  909.  
  910.           /* Is the memory operand acceptable?  */
  911.           if (flag_force_mem
  912.           || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  913.             (xop0, GET_MODE (xop0))))
  914.         {
  915.           /* No, load into a reg and extract from there.  */
  916.           enum machine_mode bestmode;
  917.  
  918.           /* Get the mode to use for inserting into this field.  If
  919.              OP0 is BLKmode, get the smallest mode consistent with the
  920.              alignment. If OP0 is a non-BLKmode object that is no
  921.              wider than MAXMODE, use its mode. Otherwise, use the
  922.              smallest mode containing the field.  */
  923.  
  924.           if (GET_MODE (xop0) == BLKmode
  925.               || (GET_MODE_SIZE (GET_MODE (op0))
  926.               > GET_MODE_SIZE (maxmode)))
  927.             bestmode = get_best_mode (bitsize, bitnum,
  928.                           align * BITS_PER_UNIT, maxmode,
  929.                           MEM_VOLATILE_P (xop0));
  930.           else
  931.             bestmode = GET_MODE (xop0);
  932.  
  933.           if (bestmode == VOIDmode)
  934.             goto extzv_loses;
  935.  
  936.           /* Compute offset as multiple of this unit,
  937.              counting in bytes.  */
  938.           unit = GET_MODE_BITSIZE (bestmode);
  939.           xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  940.           xbitpos = bitnum % unit;
  941.           xop0 = change_address (xop0, bestmode,
  942.                      plus_constant (XEXP (xop0, 0),
  943.                             xoffset));
  944.           /* Fetch it to a register in that size.  */
  945.           xop0 = force_reg (bestmode, xop0);
  946.  
  947.           /* XBITPOS counts within UNIT, which is what is expected.  */
  948.         }
  949.           else
  950.         /* Get ref to first byte containing part of the field.  */
  951.         xop0 = change_address (xop0, byte_mode,
  952.                        plus_constant (XEXP (xop0, 0), xoffset));
  953.  
  954.           volatile_ok = save_volatile_ok;
  955.         }
  956.  
  957.       /* If op0 is a register, we need it in MAXMODE (which is usually
  958.          SImode). to make it acceptable to the format of extzv.  */
  959.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
  960.         abort ();
  961.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  962.         xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  963.  
  964.       /* On big-endian machines, we count bits from the most significant.
  965.          If the bit field insn does not, we must invert.  */
  966. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  967.       xbitpos = unit - bitsize - xbitpos;
  968. #endif
  969.       /* Now convert from counting within UNIT to counting in MAXMODE.  */
  970. #if BITS_BIG_ENDIAN
  971.       if (GET_CODE (xop0) != MEM)
  972.         xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
  973. #endif
  974.       unit = GET_MODE_BITSIZE (maxmode);
  975.  
  976.       if (xtarget == 0
  977.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  978.         xtarget = xspec_target = gen_reg_rtx (tmode);
  979.  
  980.       if (GET_MODE (xtarget) != maxmode)
  981.         {
  982.           if (GET_CODE (xtarget) == REG)
  983.         {
  984.           int wider = (GET_MODE_SIZE (maxmode)
  985.                    > GET_MODE_SIZE (GET_MODE (xtarget)));
  986.           xtarget = gen_lowpart (maxmode, xtarget);
  987.           if (wider)
  988.             xspec_target_subreg = xtarget;
  989.         }
  990.           else
  991.         xtarget = gen_reg_rtx (maxmode);
  992.         }
  993.  
  994.       /* If this machine's extzv insists on a register target,
  995.          make sure we have one.  */
  996.       if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  997.          (xtarget, maxmode)))
  998.         xtarget = gen_reg_rtx (maxmode);
  999.  
  1000.       bitsize_rtx = GEN_INT (bitsize);
  1001.       bitpos_rtx = GEN_INT (xbitpos);
  1002.  
  1003.       pat = gen_extzv (protect_from_queue (xtarget, 1),
  1004.                xop0, bitsize_rtx, bitpos_rtx);
  1005.       if (pat)
  1006.         {
  1007.           emit_insn (pat);
  1008.           target = xtarget;
  1009.           spec_target = xspec_target;
  1010.           spec_target_subreg = xspec_target_subreg;
  1011.         }
  1012.       else
  1013.         {
  1014.           delete_insns_since (last);
  1015.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  1016.                         bitpos, target, 1, align);
  1017.         }
  1018.     }
  1019.       else
  1020.         extzv_loses:
  1021. #endif
  1022.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1023.                       target, 1, align);
  1024.     }
  1025.   else
  1026.     {
  1027. #ifdef HAVE_extv
  1028.       if (HAVE_extv
  1029.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0])
  1030.           >= bitsize))
  1031.     {
  1032.       int xbitpos = bitpos, xoffset = offset;
  1033.       rtx bitsize_rtx, bitpos_rtx;
  1034.       rtx last = get_last_insn();
  1035.       rtx xop0 = op0, xtarget = target;
  1036.       rtx xspec_target = spec_target;
  1037.       rtx xspec_target_subreg = spec_target_subreg;
  1038.       rtx pat;
  1039.       enum machine_mode maxmode
  1040.         = insn_operand_mode[(int) CODE_FOR_extv][0];
  1041.  
  1042.       if (GET_CODE (xop0) == MEM)
  1043.         {
  1044.           /* Is the memory operand acceptable?  */
  1045.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
  1046.              (xop0, GET_MODE (xop0))))
  1047.         {
  1048.           /* No, load into a reg and extract from there.  */
  1049.           enum machine_mode bestmode;
  1050.  
  1051.           /* Get the mode to use for inserting into this field.  If
  1052.              OP0 is BLKmode, get the smallest mode consistent with the
  1053.              alignment. If OP0 is a non-BLKmode object that is no
  1054.              wider than MAXMODE, use its mode. Otherwise, use the
  1055.              smallest mode containing the field.  */
  1056.  
  1057.           if (GET_MODE (xop0) == BLKmode
  1058.               || (GET_MODE_SIZE (GET_MODE (op0))
  1059.               > GET_MODE_SIZE (maxmode)))
  1060.             bestmode = get_best_mode (bitsize, bitnum,
  1061.                           align * BITS_PER_UNIT, maxmode,
  1062.                           MEM_VOLATILE_P (xop0));
  1063.           else
  1064.             bestmode = GET_MODE (xop0);
  1065.  
  1066.           if (bestmode == VOIDmode)
  1067.             goto extv_loses;
  1068.  
  1069.           /* Compute offset as multiple of this unit,
  1070.              counting in bytes.  */
  1071.           unit = GET_MODE_BITSIZE (bestmode);
  1072.           xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  1073.           xbitpos = bitnum % unit;
  1074.           xop0 = change_address (xop0, bestmode,
  1075.                      plus_constant (XEXP (xop0, 0),
  1076.                             xoffset));
  1077.           /* Fetch it to a register in that size.  */
  1078.           xop0 = force_reg (bestmode, xop0);
  1079.  
  1080.           /* XBITPOS counts within UNIT, which is what is expected.  */
  1081.         }
  1082.           else
  1083.         /* Get ref to first byte containing part of the field.  */
  1084.         xop0 = change_address (xop0, byte_mode,
  1085.                        plus_constant (XEXP (xop0, 0), xoffset));
  1086.         }
  1087.  
  1088.       /* If op0 is a register, we need it in MAXMODE (which is usually
  1089.          SImode) to make it acceptable to the format of extv.  */
  1090.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
  1091.         abort ();
  1092.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  1093.         xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  1094.  
  1095.       /* On big-endian machines, we count bits from the most significant.
  1096.          If the bit field insn does not, we must invert.  */
  1097. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  1098.       xbitpos = unit - bitsize - xbitpos;
  1099. #endif
  1100.       /* XBITPOS counts within a size of UNIT.
  1101.          Adjust to count within a size of MAXMODE.  */
  1102. #if BITS_BIG_ENDIAN
  1103.       if (GET_CODE (xop0) != MEM)
  1104.         xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
  1105. #endif
  1106.       unit = GET_MODE_BITSIZE (maxmode);
  1107.  
  1108.       if (xtarget == 0
  1109.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  1110.         xtarget = xspec_target = gen_reg_rtx (tmode);
  1111.  
  1112.       if (GET_MODE (xtarget) != maxmode)
  1113.         {
  1114.           if (GET_CODE (xtarget) == REG)
  1115.         {
  1116.           int wider = (GET_MODE_SIZE (maxmode)
  1117.                    > GET_MODE_SIZE (GET_MODE (xtarget)));
  1118.           xtarget = gen_lowpart (maxmode, xtarget);
  1119.           if (wider)
  1120.             xspec_target_subreg = xtarget;
  1121.         }
  1122.           else
  1123.         xtarget = gen_reg_rtx (maxmode);
  1124.         }
  1125.  
  1126.       /* If this machine's extv insists on a register target,
  1127.          make sure we have one.  */
  1128.       if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][0])
  1129.          (xtarget, maxmode)))
  1130.         xtarget = gen_reg_rtx (maxmode);
  1131.  
  1132.       bitsize_rtx = GEN_INT (bitsize);
  1133.       bitpos_rtx = GEN_INT (xbitpos);
  1134.  
  1135.       pat = gen_extv (protect_from_queue (xtarget, 1),
  1136.               xop0, bitsize_rtx, bitpos_rtx);
  1137.       if (pat)
  1138.         {
  1139.           emit_insn (pat);
  1140.           target = xtarget;
  1141.           spec_target = xspec_target;
  1142.           spec_target_subreg = xspec_target_subreg;
  1143.         }
  1144.       else
  1145.         {
  1146.           delete_insns_since (last);
  1147.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  1148.                         bitpos, target, 0, align);
  1149.         }
  1150.     } 
  1151.       else
  1152.     extv_loses:
  1153. #endif
  1154.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1155.                       target, 0, align);
  1156.     }
  1157.   if (target == spec_target)
  1158.     return target;
  1159.   if (target == spec_target_subreg)
  1160.     return spec_target;
  1161.   if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
  1162.     {
  1163.       /* If the target mode is floating-point, first convert to the
  1164.      integer mode of that size and then access it as a floating-point
  1165.      value via a SUBREG.  */
  1166.       if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
  1167.     {
  1168.       target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
  1169.                            MODE_INT, 0),
  1170.                     target, unsignedp);
  1171.       if (GET_CODE (target) != REG)
  1172.         target = copy_to_reg (target);
  1173.       return gen_rtx (SUBREG, tmode, target, 0);
  1174.     }
  1175.       else
  1176.     return convert_to_mode (tmode, target, unsignedp);
  1177.     }
  1178.   return target;
  1179. }
  1180.  
  1181. /* Extract a bit field using shifts and boolean operations
  1182.    Returns an rtx to represent the value.
  1183.    OP0 addresses a register (word) or memory (byte).
  1184.    BITPOS says which bit within the word or byte the bit field starts in.
  1185.    OFFSET says how many bytes farther the bit field starts;
  1186.     it is 0 if OP0 is a register.
  1187.    BITSIZE says how many bits long the bit field is.
  1188.     (If OP0 is a register, it may be narrower than a full word,
  1189.      but BITPOS still counts within a full word,
  1190.      which is significant on bigendian machines.)
  1191.  
  1192.    UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  1193.    If TARGET is nonzero, attempts to store the value there
  1194.    and return TARGET, but this is not guaranteed.
  1195.    If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
  1196.  
  1197.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.  */
  1198.  
  1199. static rtx
  1200. extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1201.              target, unsignedp, align)
  1202.      enum machine_mode tmode;
  1203.      register rtx op0, target;
  1204.      register int offset, bitsize, bitpos;
  1205.      int unsignedp;
  1206.      int align;
  1207. {
  1208.   int total_bits = BITS_PER_WORD;
  1209.   enum machine_mode mode;
  1210.  
  1211.   if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
  1212.     {
  1213.       /* Special treatment for a bit field split across two registers.  */
  1214.       if (bitsize + bitpos > BITS_PER_WORD)
  1215.     return extract_split_bit_field (op0, bitsize, bitpos,
  1216.                     unsignedp, align);
  1217.     }
  1218.   else
  1219.     {
  1220.       /* Get the proper mode to use for this field.  We want a mode that
  1221.      includes the entire field.  If such a mode would be larger than
  1222.      a word, we won't be doing the extraction the normal way.  */
  1223.  
  1224.       mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
  1225.                 align * BITS_PER_UNIT, word_mode,
  1226.                 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
  1227.  
  1228.       if (mode == VOIDmode)
  1229.     /* The only way this should occur is if the field spans word
  1230.        boundaries.  */
  1231.     return extract_split_bit_field (op0, bitsize,
  1232.                     bitpos + offset * BITS_PER_UNIT,
  1233.                     unsignedp, align);
  1234.  
  1235.       total_bits = GET_MODE_BITSIZE (mode);
  1236.  
  1237.       /* Get ref to an aligned byte, halfword, or word containing the field.
  1238.      Adjust BITPOS to be position within a word,
  1239.      and OFFSET to be the offset of that word.
  1240.      Then alter OP0 to refer to that word.  */
  1241.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  1242.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  1243.       op0 = change_address (op0, mode,
  1244.                 plus_constant (XEXP (op0, 0), offset));
  1245.     }
  1246.  
  1247.   mode = GET_MODE (op0);
  1248.  
  1249. #if BYTES_BIG_ENDIAN
  1250.   /* BITPOS is the distance between our msb and that of OP0.
  1251.      Convert it to the distance from the lsb.  */
  1252.  
  1253.   bitpos = total_bits - bitsize - bitpos;
  1254. #endif
  1255.   /* Now BITPOS is always the distance between the field's lsb and that of OP0.
  1256.      We have reduced the big-endian case to the little-endian case.  */
  1257.  
  1258.   if (unsignedp)
  1259.     {
  1260.       if (bitpos)
  1261.     {
  1262.       /* If the field does not already start at the lsb,
  1263.          shift it so it does.  */
  1264.       tree amount = build_int_2 (bitpos, 0);
  1265.       /* Maybe propagate the target for the shift.  */
  1266.       /* But not if we will return it--could confuse integrate.c.  */
  1267.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  1268.                && !REG_FUNCTION_VALUE_P (target)
  1269.                ? target : 0);
  1270.       if (tmode != mode) subtarget = 0;
  1271.       op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1272.     }
  1273.       /* Convert the value to the desired mode.  */
  1274.       if (mode != tmode)
  1275.     op0 = convert_to_mode (tmode, op0, 1);
  1276.  
  1277.       /* Unless the msb of the field used to be the msb when we shifted,
  1278.      mask out the upper bits.  */
  1279.  
  1280.       if (GET_MODE_BITSIZE (mode) != bitpos + bitsize
  1281. #if 0
  1282. #ifdef SLOW_ZERO_EXTEND
  1283.       /* Always generate an `and' if
  1284.          we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
  1285.          will combine fruitfully with the zero-extend. */
  1286.       || tmode != mode
  1287. #endif
  1288. #endif
  1289.       )
  1290.     return expand_binop (GET_MODE (op0), and_optab, op0,
  1291.                  mask_rtx (GET_MODE (op0), 0, bitsize, 0),
  1292.                  target, 1, OPTAB_LIB_WIDEN);
  1293.       return op0;
  1294.     }
  1295.  
  1296.   /* To extract a signed bit-field, first shift its msb to the msb of the word,
  1297.      then arithmetic-shift its lsb to the lsb of the word.  */
  1298.   op0 = force_reg (mode, op0);
  1299.   if (mode != tmode)
  1300.     target = 0;
  1301.  
  1302.   /* Find the narrowest integer mode that contains the field.  */
  1303.  
  1304.   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
  1305.        mode = GET_MODE_WIDER_MODE (mode))
  1306.     if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
  1307.       {
  1308.     op0 = convert_to_mode (mode, op0, 0);
  1309.     break;
  1310.       }
  1311.  
  1312.   if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
  1313.     {
  1314.       tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
  1315.       /* Maybe propagate the target for the shift.  */
  1316.       /* But not if we will return the result--could confuse integrate.c.  */
  1317.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  1318.                && ! REG_FUNCTION_VALUE_P (target)
  1319.                ? target : 0);
  1320.       op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1321.     }
  1322.  
  1323.   return expand_shift (RSHIFT_EXPR, mode, op0,
  1324.                build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0), 
  1325.                target, 0);
  1326. }
  1327.  
  1328. /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
  1329.    of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
  1330.    complement of that if COMPLEMENT.  The mask is truncated if
  1331.    necessary to the width of mode MODE.  */
  1332.  
  1333. static rtx
  1334. mask_rtx (mode, bitpos, bitsize, complement)
  1335.      enum machine_mode mode;
  1336.      int bitpos, bitsize, complement;
  1337. {
  1338.   HOST_WIDE_INT masklow, maskhigh;
  1339.  
  1340.   if (bitpos < HOST_BITS_PER_WIDE_INT)
  1341.     masklow = (HOST_WIDE_INT) -1 << bitpos;
  1342.   else
  1343.     masklow = 0;
  1344.  
  1345.   if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
  1346.     masklow &= ((unsigned HOST_WIDE_INT) -1
  1347.         >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
  1348.   
  1349.   if (bitpos <= HOST_BITS_PER_WIDE_INT)
  1350.     maskhigh = -1;
  1351.   else
  1352.     maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
  1353.  
  1354.   if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
  1355.     maskhigh &= ((unsigned HOST_WIDE_INT) -1
  1356.          >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
  1357.   else
  1358.     maskhigh = 0;
  1359.  
  1360.   if (complement)
  1361.     {
  1362.       maskhigh = ~maskhigh;
  1363.       masklow = ~masklow;
  1364.     }
  1365.  
  1366.   return immed_double_const (masklow, maskhigh, mode);
  1367. }
  1368.  
  1369. /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
  1370.    VALUE truncated to BITSIZE bits and then shifted left BITPOS bits.  */
  1371.  
  1372. static rtx
  1373. lshift_value (mode, value, bitpos, bitsize)
  1374.      enum machine_mode mode;
  1375.      rtx value;
  1376.      int bitpos, bitsize;
  1377. {
  1378.   unsigned HOST_WIDE_INT v = INTVAL (value);
  1379.   HOST_WIDE_INT low, high;
  1380.  
  1381.   if (bitsize < HOST_BITS_PER_WIDE_INT)
  1382.     v &= ~((HOST_WIDE_INT) -1 << bitsize);
  1383.  
  1384.   if (bitpos < HOST_BITS_PER_WIDE_INT)
  1385.     {
  1386.       low = v << bitpos;
  1387.       high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
  1388.     }
  1389.   else
  1390.     {
  1391.       low = 0;
  1392.       high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
  1393.     }
  1394.  
  1395.   return immed_double_const (low, high, mode);
  1396. }
  1397.  
  1398. /* Extract a bit field that is split across two words
  1399.    and return an RTX for the result.
  1400.  
  1401.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  1402.    BITSIZE is the field width; BITPOS, position of its first bit, in the word.
  1403.    UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.  */
  1404.  
  1405. static rtx
  1406. extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
  1407.      rtx op0;
  1408.      int bitsize, bitpos, unsignedp, align;
  1409. {
  1410.   /* BITSIZE_1 is size of the part in the first word.  */
  1411.   int bitsize_1 = BITS_PER_WORD - bitpos % BITS_PER_WORD;
  1412.   /* BITSIZE_2 is size of the rest (in the following word).  */
  1413.   int bitsize_2 = bitsize - bitsize_1;
  1414.   rtx part1, part2, result;
  1415.   int unit = GET_CODE (op0) == MEM ? BITS_PER_UNIT : BITS_PER_WORD;
  1416.   int offset = bitpos / unit;
  1417.   rtx word;
  1418.  
  1419.   /* The field must span exactly one word boundary.  */
  1420.   if (bitpos / BITS_PER_WORD != (bitpos + bitsize - 1) / BITS_PER_WORD - 1)
  1421.     abort ();
  1422.  
  1423.   /* Get the part of the bit field from the first word.  If OP0 is a MEM,
  1424.      pass OP0 and the offset computed above.  Otherwise, get the proper
  1425.      word and pass an offset of zero.  */
  1426.   word = (GET_CODE (op0) == MEM ? op0
  1427.       : operand_subword_force (op0, offset, GET_MODE (op0)));
  1428.   part1 = extract_fixed_bit_field (word_mode, word,
  1429.                    GET_CODE (op0) == MEM ? offset : 0,
  1430.                    bitsize_1, bitpos % unit, NULL_RTX,
  1431.                    1, align);
  1432.  
  1433.   /* Offset op0 by 1 word to get to the following one.  */
  1434.   if (GET_CODE (op0) == SUBREG)
  1435.     word = operand_subword_force (SUBREG_REG (op0),
  1436.                   SUBREG_WORD (op0) + offset + 1, VOIDmode);
  1437.   else if (GET_CODE (op0) == MEM)
  1438.     word = op0;
  1439.   else
  1440.     word = operand_subword_force (op0, offset + 1, GET_MODE (op0));
  1441.  
  1442.   /* Get the part of the bit field from the second word.  */
  1443.   part2 = extract_fixed_bit_field (word_mode, word,
  1444.                    (GET_CODE (op0) == MEM
  1445.                     ? CEIL (offset + 1, UNITS_PER_WORD) * UNITS_PER_WORD
  1446.                     : 0),
  1447.                    bitsize_2, 0, NULL_RTX, 1, align);
  1448.  
  1449.   /* Shift the more significant part up to fit above the other part.  */
  1450. #if BYTES_BIG_ENDIAN
  1451.   part1 = expand_shift (LSHIFT_EXPR, word_mode, part1,
  1452.             build_int_2 (bitsize_2, 0), 0, 1);
  1453. #else
  1454.   part2 = expand_shift (LSHIFT_EXPR, word_mode, part2,
  1455.             build_int_2 (bitsize_1, 0), 0, 1);
  1456. #endif
  1457.  
  1458.   /* Combine the two parts with bitwise or.  This works
  1459.      because we extracted both parts as unsigned bit fields.  */
  1460.   result = expand_binop (word_mode, ior_optab, part1, part2, NULL_RTX, 1,
  1461.              OPTAB_LIB_WIDEN);
  1462.  
  1463.   /* Unsigned bit field: we are done.  */
  1464.   if (unsignedp)
  1465.     return result;
  1466.   /* Signed bit field: sign-extend with two arithmetic shifts.  */
  1467.   result = expand_shift (LSHIFT_EXPR, word_mode, result,
  1468.              build_int_2 (BITS_PER_WORD - bitsize, 0),
  1469.              NULL_RTX, 0);
  1470.   return expand_shift (RSHIFT_EXPR, word_mode, result,
  1471.                build_int_2 (BITS_PER_WORD - bitsize, 0), NULL_RTX, 0);
  1472. }
  1473.  
  1474. /* Add INC into TARGET.  */
  1475.  
  1476. void
  1477. expand_inc (target, inc)
  1478.      rtx target, inc;
  1479. {
  1480.   rtx value = expand_binop (GET_MODE (target), add_optab,
  1481.                 target, inc,
  1482.                 target, 0, OPTAB_LIB_WIDEN);
  1483.   if (value != target)
  1484.     emit_move_insn (target, value);
  1485. }
  1486.  
  1487. /* Subtract DEC from TARGET.  */
  1488.  
  1489. void
  1490. expand_dec (target, dec)
  1491.      rtx target, dec;
  1492. {
  1493.   rtx value = expand_binop (GET_MODE (target), sub_optab,
  1494.                 target, dec,
  1495.                 target, 0, OPTAB_LIB_WIDEN);
  1496.   if (value != target)
  1497.     emit_move_insn (target, value);
  1498. }
  1499.  
  1500. /* Output a shift instruction for expression code CODE,
  1501.    with SHIFTED being the rtx for the value to shift,
  1502.    and AMOUNT the tree for the amount to shift by.
  1503.    Store the result in the rtx TARGET, if that is convenient.
  1504.    If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  1505.    Return the rtx for where the value is.  */
  1506.  
  1507. rtx
  1508. expand_shift (code, mode, shifted, amount, target, unsignedp)
  1509.      enum tree_code code;
  1510.      register enum machine_mode mode;
  1511.      rtx shifted;
  1512.      tree amount;
  1513.      register rtx target;
  1514.      int unsignedp;
  1515. {
  1516.   register rtx op1, temp = 0;
  1517.   register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  1518.   register int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
  1519.   int try;
  1520.  
  1521.   /* Previously detected shift-counts computed by NEGATE_EXPR
  1522.      and shifted in the other direction; but that does not work
  1523.      on all machines.  */
  1524.  
  1525.   op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
  1526.  
  1527.   if (op1 == const0_rtx)
  1528.     return shifted;
  1529.  
  1530.   for (try = 0; temp == 0 && try < 3; try++)
  1531.     {
  1532.       enum optab_methods methods;
  1533.  
  1534.       if (try == 0)
  1535.     methods = OPTAB_DIRECT;
  1536.       else if (try == 1)
  1537.     methods = OPTAB_WIDEN;
  1538.       else
  1539.     methods = OPTAB_LIB_WIDEN;
  1540.  
  1541.       if (rotate)
  1542.     {
  1543.       /* Widening does not work for rotation.  */
  1544.       if (methods == OPTAB_WIDEN)
  1545.         continue;
  1546.       else if (methods == OPTAB_LIB_WIDEN)
  1547.         methods = OPTAB_LIB;
  1548.  
  1549.       temp = expand_binop (mode,
  1550.                    left ? rotl_optab : rotr_optab,
  1551.                    shifted, op1, target, unsignedp, methods);
  1552.     }
  1553.       else if (unsignedp)
  1554.     {
  1555.       temp = expand_binop (mode,
  1556.                    left ? lshl_optab : lshr_optab,
  1557.                    shifted, op1, target, unsignedp, methods);
  1558.       if (temp == 0 && left)
  1559.         temp = expand_binop (mode, ashl_optab,
  1560.                  shifted, op1, target, unsignedp, methods);
  1561.     }
  1562.  
  1563.       /* Do arithmetic shifts.
  1564.      Also, if we are going to widen the operand, we can just as well
  1565.      use an arithmetic right-shift instead of a logical one.  */
  1566.       if (temp == 0 && ! rotate
  1567.       && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
  1568.     {
  1569.       enum optab_methods methods1 = methods;
  1570.  
  1571.       /* If trying to widen a log shift to an arithmetic shift,
  1572.          don't accept an arithmetic shift of the same size.  */
  1573.       if (unsignedp)
  1574.         methods1 = OPTAB_MUST_WIDEN;
  1575.  
  1576.       /* Arithmetic shift */
  1577.  
  1578.       temp = expand_binop (mode,
  1579.                    left ? ashl_optab : ashr_optab,
  1580.                    shifted, op1, target, unsignedp, methods1);
  1581.     }
  1582.  
  1583. #ifdef HAVE_extzv
  1584.       /* We can do a logical (unsigned) right shift with a bit-field
  1585.      extract insn.  But first check if one of the above methods worked.  */
  1586.       if (temp != 0)
  1587.     return temp;
  1588.  
  1589.       if (unsignedp && code == RSHIFT_EXPR && ! BITS_BIG_ENDIAN && HAVE_extzv)
  1590.     {
  1591.       enum machine_mode output_mode
  1592.         = insn_operand_mode[(int) CODE_FOR_extzv][0];
  1593.  
  1594.       if ((methods == OPTAB_DIRECT && mode == output_mode)
  1595.           || (methods == OPTAB_WIDEN
  1596.           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (output_mode)))
  1597.         {
  1598.           rtx shifted1 = convert_to_mode (output_mode,
  1599.                           protect_from_queue (shifted, 0),
  1600.                           1);
  1601.           enum machine_mode length_mode
  1602.         = insn_operand_mode[(int) CODE_FOR_extzv][2];
  1603.           enum machine_mode pos_mode
  1604.         = insn_operand_mode[(int) CODE_FOR_extzv][3];
  1605.           rtx target1 = 0;
  1606.           rtx last = get_last_insn ();
  1607.           rtx width;
  1608.           rtx xop1 = op1;
  1609.           rtx pat;
  1610.  
  1611.           if (target != 0)
  1612.         target1 = protect_from_queue (target, 1);
  1613.  
  1614.           /* We define extract insns as having OUTPUT_MODE in a register
  1615.          and the mode of operand 1 in memory.  Since we want
  1616.          OUTPUT_MODE, we will always force the operand into a
  1617.          register.  At some point we might want to support MEM
  1618.          directly. */
  1619.           shifted1 = force_reg (output_mode, shifted1);
  1620.  
  1621.           /* If we don't have or cannot use a suggested target,
  1622.          make a place for the result, in the proper mode.  */
  1623.           if (methods == OPTAB_WIDEN || target1 == 0
  1624.           || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  1625.             (target1, output_mode)))
  1626.         target1 = gen_reg_rtx (output_mode);
  1627.  
  1628.           xop1 = protect_from_queue (xop1, 0);
  1629.           xop1 = convert_to_mode (pos_mode, xop1,
  1630.                       TREE_UNSIGNED (TREE_TYPE (amount)));
  1631.  
  1632.           /* If this machine's extzv insists on a register for
  1633.          operand 3 (position), arrange for that.  */
  1634.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][3])
  1635.              (xop1, pos_mode)))
  1636.         xop1 = force_reg (pos_mode, xop1);
  1637.  
  1638.           /* WIDTH gets the width of the bit field to extract:
  1639.          wordsize minus # bits to shift by.  */
  1640.           if (GET_CODE (xop1) == CONST_INT)
  1641.         width = GEN_INT (GET_MODE_BITSIZE (mode) - INTVAL (op1));
  1642.           else
  1643.         {
  1644.           /* Now get the width in the proper mode.  */
  1645.           op1 = protect_from_queue (op1, 0);
  1646.           width = convert_to_mode (length_mode, op1,
  1647.                        TREE_UNSIGNED (TREE_TYPE (amount)));
  1648.  
  1649.           width = expand_binop (length_mode, sub_optab,
  1650.                     GEN_INT (GET_MODE_BITSIZE (mode)),
  1651.                     width, NULL_RTX, 0, OPTAB_LIB_WIDEN);
  1652.         }
  1653.  
  1654.           /* If this machine's extzv insists on a register for
  1655.          operand 2 (length), arrange for that.  */
  1656.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][2])
  1657.              (width, length_mode)))
  1658.         width = force_reg (length_mode, width);
  1659.  
  1660.           /* Now extract with WIDTH, omitting OP1 least sig bits.  */
  1661.           pat = gen_extzv (target1, shifted1, width, xop1);
  1662.           if (pat)
  1663.         {
  1664.           emit_insn (pat);
  1665.           temp = convert_to_mode (mode, target1, 1);
  1666.         }
  1667.           else
  1668.         delete_insns_since (last);
  1669.         }
  1670.  
  1671.       /* Can also do logical shift with signed bit-field extract
  1672.          followed by inserting the bit-field at a different position.
  1673.          That strategy is not yet implemented.  */
  1674.     }
  1675. #endif /* HAVE_extzv */
  1676.     }
  1677.  
  1678.   if (temp == 0)
  1679.     abort ();
  1680.   return temp;
  1681. }
  1682.  
  1683. enum alg_code { alg_add, alg_subtract, alg_compound };
  1684.  
  1685. /* This structure records a sequence of operations.
  1686.    `ops' is the number of operations recorded.
  1687.    `cost' is their total cost.
  1688.    The operations are stored in `op' and the corresponding
  1689.    integer coefficients in `coeff'.
  1690.    These are the operations:
  1691.    alg_add       Add to the total the multiplicand times the coefficient.
  1692.    alg_subtract  Subtract the multiplicand times the coefficient.
  1693.    alg_compound  This coefficient plus or minus the following one
  1694.                  is multiplied into the total.  The following operation
  1695.                  is alg_add or alg_subtract to indicate whether to add
  1696.          or subtract the two coefficients.  */
  1697.  
  1698. #ifndef MAX_BITS_PER_WORD
  1699. #define MAX_BITS_PER_WORD BITS_PER_WORD
  1700. #endif
  1701.  
  1702. struct algorithm
  1703. {
  1704.   int cost;
  1705.   unsigned int ops;
  1706.   enum alg_code op[MAX_BITS_PER_WORD];
  1707.   unsigned HOST_WIDE_INT coeff[MAX_BITS_PER_WORD];
  1708. };
  1709.  
  1710. /* Compute and return the best algorithm for multiplying by T.
  1711.    Assume that add insns cost ADD_COST and shifts cost SHIFT_COST.
  1712.    Return cost -1 if would cost more than MAX_COST.  */
  1713.  
  1714. static struct algorithm
  1715. synth_mult (t, add_cost, shift_cost, max_cost)
  1716.      unsigned HOST_WIDE_INT t;
  1717.      int add_cost, shift_cost;
  1718.      int max_cost;
  1719. {
  1720.   int m, n;
  1721.   struct algorithm *best_alg
  1722.     = (struct algorithm *)alloca (sizeof (struct algorithm));
  1723.   struct algorithm *alg_in
  1724.     = (struct algorithm *)alloca (sizeof (struct algorithm));
  1725.   unsigned int cost;
  1726.  
  1727.   /* No matter what happens, we want to return a valid algorithm.  */
  1728.   best_alg->cost = max_cost;
  1729.   best_alg->ops = 0;
  1730.  
  1731.   /* Is t an exponent of 2, so we can just do a shift?  */
  1732.  
  1733.   if ((t & -t) == t)
  1734.     {
  1735.       if (t > 1)
  1736.     {
  1737.       if (max_cost >= shift_cost)
  1738.         {
  1739.           best_alg->cost = shift_cost;
  1740.           best_alg->ops = 1;
  1741.           best_alg->op[0] = alg_add;
  1742.           best_alg->coeff[0] = t;
  1743.         }
  1744.       else
  1745.         best_alg->cost = -1;
  1746.     }
  1747.       else if (t == 1)
  1748.     {
  1749.       if (max_cost >= 0)
  1750.         best_alg->cost = 0;
  1751.     }
  1752.       else
  1753.     best_alg->cost = 0;
  1754.  
  1755.       return *best_alg;
  1756.     }
  1757.  
  1758.   /* If MAX_COST just permits as little as an addition (or less), we won't
  1759.      succeed in synthesizing an algorithm for t.  Return immediately with
  1760.      an indication of failure.  */
  1761.   if (max_cost <= add_cost)
  1762.     {
  1763.       best_alg->cost = -1;
  1764.       return *best_alg;
  1765.     }
  1766.  
  1767.   /* Look for factors of t of the form
  1768.      t = q(2**m +- 1), 2 <= m <= floor(log2(t)) - 1.
  1769.      If we find such a factor, we can multiply by t using an algorithm that
  1770.      multiplies by q, shift the result by m and add/subtract it to itself.  */
  1771.  
  1772.   for (m = floor_log2 (t) - 1; m >= 2; m--)
  1773.     {
  1774.       HOST_WIDE_INT m_exp_2 = (HOST_WIDE_INT) 1 << m;
  1775.       HOST_WIDE_INT d;
  1776.  
  1777.       d = m_exp_2 + 1;
  1778.       if (t % d == 0)
  1779.     {
  1780.       HOST_WIDE_INT q = t / d;
  1781.  
  1782.       cost = add_cost + shift_cost * 2;
  1783.  
  1784.       *alg_in = synth_mult (q, add_cost, shift_cost,
  1785.                 MIN (max_cost, best_alg->cost) - cost);
  1786.  
  1787.       if (alg_in->cost >= 0)
  1788.         {
  1789.           cost += alg_in->cost;
  1790.  
  1791.           if (cost < best_alg->cost)
  1792.         {
  1793.           struct algorithm *x;
  1794.           x = alg_in;
  1795.           alg_in = best_alg;
  1796.           best_alg = x;
  1797.           best_alg->coeff[best_alg->ops] = m_exp_2;
  1798.           best_alg->op[best_alg->ops++] = alg_compound;
  1799.           best_alg->coeff[best_alg->ops] = 1;
  1800.           best_alg->op[best_alg->ops++] = alg_add;
  1801.           best_alg->cost = cost;
  1802.         }
  1803.         }
  1804.     }
  1805.  
  1806.       d = m_exp_2 - 1;
  1807.       if (t % d == 0)
  1808.     {
  1809.       HOST_WIDE_INT q = t / d;
  1810.  
  1811.       cost = add_cost + shift_cost * 2;
  1812.  
  1813.       *alg_in = synth_mult (q, add_cost, shift_cost,
  1814.                 MIN (max_cost, best_alg->cost) - cost);
  1815.  
  1816.       if (alg_in->cost >= 0)
  1817.         {
  1818.           cost += alg_in->cost;
  1819.  
  1820.           if (cost < best_alg->cost)
  1821.         {
  1822.           struct algorithm *x;
  1823.           x = alg_in;
  1824.           alg_in = best_alg;
  1825.           best_alg = x;
  1826.           best_alg->coeff[best_alg->ops] = m_exp_2;
  1827.           best_alg->op[best_alg->ops++] = alg_compound;
  1828.           best_alg->coeff[best_alg->ops] = 1;
  1829.           best_alg->op[best_alg->ops++] = alg_subtract;
  1830.           best_alg->cost = cost;
  1831.         }
  1832.         }
  1833.     }
  1834.     }
  1835.  
  1836.   /* Try load effective address instructions, i.e. do a*3, a*5, a*9.  */
  1837.  
  1838.   {
  1839.     HOST_WIDE_INT q;
  1840.     HOST_WIDE_INT w;
  1841.  
  1842.     q = t & -t;            /* get out lsb */
  1843.     w = (t - q) & -(t - q);    /* get out next lsb */
  1844.  
  1845.     if (w / q <= lea_max_mul)
  1846.       {
  1847.     cost = lea_cost + (q != 1 ? shift_cost : 0);
  1848.  
  1849.     *alg_in = synth_mult (t - q - w, add_cost, shift_cost,
  1850.                   MIN (max_cost, best_alg->cost) - cost);
  1851.  
  1852.     if (alg_in->cost >= 0)
  1853.       {
  1854.         cost += alg_in->cost;
  1855.  
  1856.         /* Use <= to prefer this method to the factoring method
  1857.            when the cost appears the same, because this method
  1858.            uses fewer temporary registers.  */
  1859.         if (cost <= best_alg->cost)
  1860.           {
  1861.         struct algorithm *x;
  1862.         x = alg_in;
  1863.         alg_in = best_alg;
  1864.         best_alg = x;
  1865.         best_alg->coeff[best_alg->ops] = w;
  1866.         best_alg->op[best_alg->ops++] = alg_add;
  1867.         best_alg->coeff[best_alg->ops] = q;
  1868.         best_alg->op[best_alg->ops++] = alg_add;
  1869.         best_alg->cost = cost;
  1870.           }
  1871.       }
  1872.       }
  1873.   }
  1874.  
  1875.   /* Now, use the good old method to add or subtract at the leftmost
  1876.      1-bit.  */
  1877.  
  1878.   {
  1879.     HOST_WIDE_INT q;
  1880.     HOST_WIDE_INT w;
  1881.  
  1882.     q = t & -t;            /* get out lsb */
  1883.     for (w = q; (w & t) != 0; w <<= 1)
  1884.       ;
  1885.     if ((w > q << 1)
  1886.     /* Reject the case where t has only two bits.
  1887.        Thus we prefer addition in that case.  */
  1888.     && !(t < w && w == q << 2))
  1889.       {
  1890.     /* There are many bits in a row.  Make 'em by subtraction.  */
  1891.  
  1892.     cost = add_cost;
  1893.     if (q != 1)
  1894.       cost += shift_cost;
  1895.  
  1896.     *alg_in = synth_mult (t + q, add_cost, shift_cost,
  1897.                   MIN (max_cost, best_alg->cost) - cost);
  1898.  
  1899.     if (alg_in->cost >= 0)
  1900.       {
  1901.         cost += alg_in->cost;
  1902.  
  1903.         /* Use <= to prefer this method to the factoring method
  1904.            when the cost appears the same, because this method
  1905.            uses fewer temporary registers.  */
  1906.         if (cost <= best_alg->cost)
  1907.           {
  1908.         struct algorithm *x;
  1909.         x = alg_in;
  1910.         alg_in = best_alg;
  1911.         best_alg = x;
  1912.         best_alg->coeff[best_alg->ops] = q;
  1913.         best_alg->op[best_alg->ops++] = alg_subtract;
  1914.         best_alg->cost = cost;
  1915.           }
  1916.       }
  1917.       }
  1918.     else
  1919.       {
  1920.     /* There's only one bit at the left.  Make it by addition.  */
  1921.  
  1922.     cost = add_cost;
  1923.     if (q != 1)
  1924.       cost += shift_cost;
  1925.  
  1926.     *alg_in = synth_mult (t - q, add_cost, shift_cost,
  1927.                   MIN (max_cost, best_alg->cost) - cost);
  1928.  
  1929.     if (alg_in->cost >= 0)
  1930.       {
  1931.         cost += alg_in->cost;
  1932.  
  1933.         if (cost <= best_alg->cost)
  1934.           {
  1935.         struct algorithm *x;
  1936.         x = alg_in;
  1937.         alg_in = best_alg;
  1938.         best_alg = x;
  1939.         best_alg->coeff[best_alg->ops] = q;
  1940.         best_alg->op[best_alg->ops++] = alg_add;
  1941.         best_alg->cost = cost;
  1942.           }
  1943.       }
  1944.       }
  1945.   }
  1946.  
  1947.   if (best_alg->cost >= max_cost)
  1948.     best_alg->cost = -1;
  1949.   return *best_alg;
  1950. }
  1951.  
  1952. /* Perform a multiplication and return an rtx for the result.
  1953.    MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  1954.    TARGET is a suggestion for where to store the result (an rtx).
  1955.  
  1956.    We check specially for a constant integer as OP1.
  1957.    If you want this check for OP0 as well, then before calling
  1958.    you should swap the two operands if OP0 would be constant.  */
  1959.  
  1960. rtx
  1961. expand_mult (mode, op0, op1, target, unsignedp)
  1962.      enum machine_mode mode;
  1963.      register rtx op0, op1, target;
  1964.      int unsignedp;
  1965. {
  1966.   rtx const_op1 = op1;
  1967.  
  1968.   /* If we are multiplying in DImode, it may still be a win
  1969.      to try to work with shifts and adds.  */
  1970.   if (GET_CODE (op1) == CONST_DOUBLE
  1971.       && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
  1972.       && HOST_BITS_PER_INT <= BITS_PER_WORD)
  1973.     {
  1974.       if ((CONST_DOUBLE_HIGH (op1) == 0 && CONST_DOUBLE_LOW (op1) >= 0)
  1975.       || (CONST_DOUBLE_HIGH (op1) == -1 && CONST_DOUBLE_LOW (op1) < 0))
  1976.     const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
  1977.     }
  1978.  
  1979.   /* We used to test optimize here, on the grounds that it's better to
  1980.      produce a smaller program when -O is not used.
  1981.      But this causes such a terrible slowdown sometimes
  1982.      that it seems better to use synth_mult always.  */
  1983.   if (GET_CODE (const_op1) == CONST_INT && ! mult_is_very_cheap)
  1984.     {
  1985.       struct algorithm alg;
  1986.       struct algorithm neg_alg;
  1987.       int negate = 0;
  1988.       HOST_WIDE_INT absval = INTVAL (op1);
  1989.       rtx last;
  1990.  
  1991.       /* Try to do the computation two ways: multiply by the negative of OP1
  1992.      and then negate, or do the multiplication directly.  The latter is
  1993.      usually faster for positive numbers and the former for negative
  1994.      numbers, but the opposite can be faster if the original value
  1995.      has a factor of 2**m +/- 1, while the negated value does not or
  1996.      vice versa.  */
  1997.  
  1998.       alg = synth_mult (absval, add_cost, shift_cost, mult_cost);
  1999.       neg_alg = synth_mult (- absval, add_cost, shift_cost,
  2000.                 (alg.cost >= 0 ? alg.cost : mult_cost)
  2001.                 - negate_cost);
  2002.  
  2003.       if (neg_alg.cost >= 0 && neg_alg.cost + negate_cost < alg.cost)
  2004.     alg = neg_alg, negate = 1, absval = - absval;
  2005.  
  2006.       if (alg.cost >= 0)
  2007.     {
  2008.       /* If we found something, it must be cheaper than multiply.
  2009.          So use it.  */
  2010.       int opno = 0;
  2011.       rtx accum, tem;
  2012.       int factors_seen = 0;
  2013.  
  2014.       op0 = protect_from_queue (op0, 0);
  2015.  
  2016.       /* Avoid referencing memory over and over.
  2017.          For speed, but also for correctness when mem is volatile.  */
  2018.       if (GET_CODE (op0) == MEM)
  2019.         op0 = force_reg (mode, op0);
  2020.  
  2021.       if (alg.ops == 0)
  2022.         accum = copy_to_mode_reg (mode, op0);
  2023.       else
  2024.         {
  2025.           /* 1 if this is the last in a series of adds and subtracts.  */
  2026.           int last = (1 == alg.ops || alg.op[1] == alg_compound);
  2027.           int log = floor_log2 (alg.coeff[0]);
  2028.           if (! factors_seen && ! last)
  2029.         log -= floor_log2 (alg.coeff[1]);
  2030.  
  2031.           if (alg.op[0] != alg_add)
  2032.         abort ();
  2033.           accum = expand_shift (LSHIFT_EXPR, mode, op0,
  2034.                     build_int_2 (log, 0), NULL_RTX, 0);
  2035.         }
  2036.    
  2037.       while (++opno < alg.ops)
  2038.         {
  2039.           int log = floor_log2 (alg.coeff[opno]);
  2040.           /* 1 if this is the last in a series of adds and subtracts.  */
  2041.           int last = (opno + 1 == alg.ops
  2042.               || alg.op[opno + 1] == alg_compound);
  2043.  
  2044.           /* If we have not yet seen any separate factors (alg_compound)
  2045.          then turn op0<<a1 + op0<<a2 + op0<<a3... into
  2046.          (op0<<(a1-a2) + op0)<<(a2-a3) + op0...  */
  2047.           switch (alg.op[opno])
  2048.         {
  2049.         case alg_add:
  2050.           if (factors_seen)
  2051.             {
  2052.               tem = expand_shift (LSHIFT_EXPR, mode, op0,
  2053.                       build_int_2 (log, 0), NULL_RTX, 0);
  2054.               accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
  2055.                          accum);
  2056.             }
  2057.           else
  2058.             {
  2059.               if (! last)
  2060.             log -= floor_log2 (alg.coeff[opno + 1]);
  2061.               accum = force_operand (gen_rtx (PLUS, mode, accum, op0),
  2062.                          accum);
  2063.               accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2064.                         build_int_2 (log, 0), accum, 0);
  2065.             }
  2066.           break;
  2067.  
  2068.         case alg_subtract:
  2069.           if (factors_seen)
  2070.             {
  2071.               tem = expand_shift (LSHIFT_EXPR, mode, op0,
  2072.                       build_int_2 (log, 0), NULL_RTX, 0);
  2073.               accum = force_operand (gen_rtx (MINUS, mode, accum, tem),
  2074.                          accum);
  2075.             }
  2076.           else
  2077.             {
  2078.               if (! last)
  2079.             log -= floor_log2 (alg.coeff[opno + 1]);
  2080.               accum = force_operand (gen_rtx (MINUS, mode, accum, op0),
  2081.                          accum);
  2082.               accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2083.                         build_int_2 (log, 0), accum, 0);
  2084.             }
  2085.  
  2086.           break;
  2087.  
  2088.         case alg_compound:
  2089.           factors_seen = 1;
  2090.           tem = expand_shift (LSHIFT_EXPR, mode, accum,
  2091.                       build_int_2 (log, 0), NULL_RTX, 0);
  2092.  
  2093.           log = floor_log2 (alg.coeff[opno + 1]);
  2094.           accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2095.                     build_int_2 (log, 0), NULL_RTX, 0);
  2096.           opno++;
  2097.           if (alg.op[opno] == alg_add)
  2098.             accum = force_operand (gen_rtx (PLUS, mode, tem, accum),
  2099.                        tem);
  2100.           else
  2101.             accum = force_operand (gen_rtx (MINUS, mode, tem, accum),
  2102.                        tem);
  2103.         }
  2104.         }
  2105.  
  2106.       /* Write a REG_EQUAL note on the last insn so that we can cse 
  2107.          multiplication sequences.  We need not do this if we were
  2108.          multiplying by a power of two, since only one insn would have
  2109.          been generated.
  2110.  
  2111.          ??? We could also write REG_EQUAL notes on the last insn of
  2112.          each sequence that uses a single temporary, but it is not
  2113.          clear how to calculate the partial product so far.
  2114.  
  2115.          Torbjorn: Can you do this?  */
  2116.  
  2117.       if (exact_log2 (absval) < 0)
  2118.         {
  2119.           last = get_last_insn ();
  2120.           REG_NOTES (last)
  2121.         = gen_rtx (EXPR_LIST, REG_EQUAL,
  2122.                gen_rtx (MULT, mode, op0, 
  2123.                     negate ? GEN_INT (absval) : op1),
  2124.                REG_NOTES (last));
  2125.         }
  2126.  
  2127.       return (negate ? expand_unop (mode, neg_optab, accum, target, 0)
  2128.           : accum);
  2129.     }
  2130.     }
  2131.  
  2132.   /* This used to use umul_optab if unsigned,
  2133.      but I think that for non-widening multiply there is no difference
  2134.      between signed and unsigned.  */
  2135.   op0 = expand_binop (mode, smul_optab,
  2136.               op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  2137.   if (op0 == 0)
  2138.     abort ();
  2139.   return op0;
  2140. }
  2141.  
  2142. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  2143.    if that is convenient, and returning where the result is.
  2144.    You may request either the quotient or the remainder as the result;
  2145.    specify REM_FLAG nonzero to get the remainder.
  2146.  
  2147.    CODE is the expression code for which kind of division this is;
  2148.    it controls how rounding is done.  MODE is the machine mode to use.
  2149.    UNSIGNEDP nonzero means do unsigned division.  */
  2150.  
  2151. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  2152.    and then correct it by or'ing in missing high bits
  2153.    if result of ANDI is nonzero.
  2154.    For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  2155.    This could optimize to a bfexts instruction.
  2156.    But C doesn't use these operations, so their optimizations are
  2157.    left for later.  */
  2158.  
  2159. rtx
  2160. expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
  2161.      int rem_flag;
  2162.      enum tree_code code;
  2163.      enum machine_mode mode;
  2164.      register rtx op0, op1, target;
  2165.      int unsignedp;
  2166. {
  2167.   register rtx result = 0;
  2168.   enum machine_mode compute_mode;
  2169.   int log = -1;
  2170.   int size;
  2171.   int can_clobber_op0;
  2172.   int mod_insn_no_good = 0;
  2173.   rtx adjusted_op0 = op0;
  2174.   optab optab1, optab2;
  2175.  
  2176.   /* We shouldn't be called with op1 == const1_rtx, but some of the
  2177.      code below will malfunction if we are, so check here and handle
  2178.      the special case if so.  */
  2179.   if (op1 == const1_rtx)
  2180.     return rem_flag ? const0_rtx : op0;
  2181.  
  2182.   /* Don't use the function value register as a target
  2183.      since we have to read it as well as write it,
  2184.      and function-inlining gets confused by this.  */
  2185.   if (target && REG_P (target) && REG_FUNCTION_VALUE_P (target))
  2186.     target = 0;
  2187.  
  2188.   /* Don't clobber an operand while doing a multi-step calculation.  */
  2189.   if (target)
  2190.     if ((rem_flag && (reg_mentioned_p (target, op0)
  2191.               || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
  2192.     || reg_mentioned_p (target, op1)
  2193.     || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM))
  2194.       target = 0;
  2195.  
  2196.   can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
  2197.  
  2198.   if (GET_CODE (op1) == CONST_INT)
  2199.     log = exact_log2 (INTVAL (op1));
  2200.  
  2201.   /* If log is >= 0, we are dividing by 2**log, and will do it by shifting,
  2202.      which is really floor-division.  Otherwise we will really do a divide,
  2203.      and we assume that is trunc-division.
  2204.  
  2205.      We must correct the dividend by adding or subtracting something
  2206.      based on the divisor, in order to do the kind of rounding specified
  2207.      by CODE.  The correction depends on what kind of rounding is actually
  2208.      available, and that depends on whether we will shift or divide.
  2209.  
  2210.      In many of these cases it is possible to perform the operation by a
  2211.      clever series of logical operations (shifts and/or exclusive-ors).
  2212.      Although avoiding the jump has the advantage that it extends the basic
  2213.      block and allows further optimization, the branch-free code is normally
  2214.      at least one instruction longer in the (most common) case where the
  2215.      dividend is non-negative.  Performance measurements of the two
  2216.      alternatives show that the branch-free code is slightly faster on the
  2217.      IBM ROMP but slower on CISC processors (significantly slower on the
  2218.      VAX).  Accordingly, the jump code has been retained.
  2219.  
  2220.      On machines where the jump code is slower, the cost of a DIV or MOD
  2221.      operation can be set small (less than twice that of an addition); in 
  2222.      that case, we pretend that we don't have a power of two and perform
  2223.      a normal division or modulus operation.  */
  2224.  
  2225.   if ((code == TRUNC_MOD_EXPR || code == TRUNC_DIV_EXPR)
  2226.       && ! unsignedp
  2227.       && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap))
  2228.     log = -1;
  2229.  
  2230.   /* Get the mode in which to perform this computation.  Normally it will
  2231.      be MODE, but sometimes we can't do the desired operation in MODE.
  2232.      If so, pick a wider mode in which we can do the operation.  Convert
  2233.      to that mode at the start to avoid repeated conversions.
  2234.  
  2235.      First see what operations we need.  These depend on the expression
  2236.      we are evaluating.  (We assume that divxx3 insns exist under the
  2237.      same conditions that modxx3 insns and that these insns don't normally
  2238.      fail.  If these assumptions are not correct, we may generate less
  2239.      efficient code in some cases.)
  2240.  
  2241.      Then see if we find a mode in which we can open-code that operation
  2242.      (either a division, modulus, or shift).  Finally, check for the smallest
  2243.      mode for which we can do the operation with a library call.  */
  2244.  
  2245.   optab1 = (log >= 0 ? (unsignedp ? lshr_optab : ashr_optab)
  2246.         : (unsignedp ? udiv_optab : sdiv_optab));
  2247.   optab2 = (log >= 0 ? optab1 : (unsignedp ? udivmod_optab : sdivmod_optab));
  2248.  
  2249.   for (compute_mode = mode; compute_mode != VOIDmode;
  2250.        compute_mode = GET_MODE_WIDER_MODE (compute_mode))
  2251.     if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
  2252.     || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
  2253.       break;
  2254.  
  2255.   if (compute_mode == VOIDmode)
  2256.     for (compute_mode = mode; compute_mode != VOIDmode;
  2257.      compute_mode = GET_MODE_WIDER_MODE (compute_mode))
  2258.       if (optab1->handlers[(int) compute_mode].libfunc
  2259.       || optab2->handlers[(int) compute_mode].libfunc)
  2260.     break;
  2261.  
  2262.   /* If we still couldn't find a mode, use MODE; we'll probably abort in
  2263.      expand_binop.  */
  2264.   if (compute_mode == VOIDmode)
  2265.     compute_mode = mode;
  2266.  
  2267.   size = GET_MODE_BITSIZE (compute_mode);
  2268.  
  2269.   /* Now convert to the best mode to use.  Show we made a copy of OP0
  2270.      and hence we can clobber it (we cannot use a SUBREG to widen
  2271.      something.  */
  2272.   if (compute_mode != mode)
  2273.     {
  2274.       adjusted_op0 = op0 = convert_to_mode (compute_mode, op0, unsignedp);
  2275.       can_clobber_op0 = 1;
  2276.       op1 = convert_to_mode (compute_mode, op1, unsignedp);
  2277.     }
  2278.  
  2279.   /* If we are computing the remainder and one of the operands is a volatile
  2280.      MEM, copy it into a register.  */
  2281.  
  2282.   if (rem_flag && GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
  2283.     adjusted_op0 = op0 = force_reg (compute_mode, op0), can_clobber_op0 = 1;
  2284.   if (rem_flag && GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
  2285.     op1 = force_reg (compute_mode, op1);
  2286.  
  2287.   /* If we are computing the remainder, op0 will be needed later to calculate
  2288.      X - Y * (X / Y), therefore cannot be clobbered. */
  2289.   if (rem_flag)
  2290.     can_clobber_op0 = 0;
  2291.  
  2292.   if (target == 0 || GET_MODE (target) != compute_mode)
  2293.     target = gen_reg_rtx (compute_mode);
  2294.  
  2295.   switch (code)
  2296.     {
  2297.     case TRUNC_MOD_EXPR:
  2298.     case TRUNC_DIV_EXPR:
  2299.       if (log >= 0 && ! unsignedp)
  2300.     {
  2301.       if (! can_clobber_op0)
  2302.         {
  2303.           adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
  2304.                             compute_mode);
  2305.           /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
  2306.          which will screw up mem refs for autoincrements.  */
  2307.           op0 = force_reg (compute_mode, op0);
  2308.         }
  2309.       /* Here we need to add OP1-1 if OP0 is negative, 0 otherwise.
  2310.          This can be computed without jumps by arithmetically shifting
  2311.          OP0 right LOG-1 places and then shifting right logically
  2312.          SIZE-LOG bits.  The resulting value is unconditionally added
  2313.          to OP0.  */
  2314.       if (log == 1 || BRANCH_COST >= 3)
  2315.         {
  2316.           rtx temp = gen_reg_rtx (compute_mode);
  2317.           temp = copy_to_suggested_reg (adjusted_op0, temp, compute_mode);
  2318.           temp = expand_shift (RSHIFT_EXPR, compute_mode, temp,
  2319.                    build_int_2 (log - 1, 0), NULL_RTX, 0);
  2320.           temp = expand_shift (RSHIFT_EXPR, compute_mode, temp,
  2321.                    build_int_2 (size - log, 0),
  2322.                    temp, 1);
  2323.           expand_inc (adjusted_op0, temp);
  2324.         }
  2325.       else
  2326.         {
  2327.           rtx label = gen_label_rtx ();
  2328.           emit_cmp_insn (adjusted_op0, const0_rtx, GE, 
  2329.                  NULL_RTX, compute_mode, 0, 0);
  2330.           emit_jump_insn (gen_bge (label));
  2331.           expand_inc (adjusted_op0, plus_constant (op1, -1));
  2332.           emit_label (label);
  2333.         }
  2334.       mod_insn_no_good = 1;
  2335.     }
  2336.       break;
  2337.  
  2338.     case FLOOR_DIV_EXPR:
  2339.     case FLOOR_MOD_EXPR:
  2340.       if (log < 0 && ! unsignedp)
  2341.     {
  2342.       rtx label = gen_label_rtx ();
  2343.       if (! can_clobber_op0)
  2344.         {
  2345.           adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
  2346.                             compute_mode);
  2347.           /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
  2348.          which will screw up mem refs for autoincrements.  */
  2349.           op0 = force_reg (compute_mode, op0);
  2350.         }
  2351.       emit_cmp_insn (adjusted_op0, const0_rtx, GE, 
  2352.              NULL_RTX, compute_mode, 0, 0);
  2353.       emit_jump_insn (gen_bge (label));
  2354.       expand_dec (adjusted_op0, op1);
  2355.       expand_inc (adjusted_op0, const1_rtx);
  2356.       emit_label (label);
  2357.       mod_insn_no_good = 1;
  2358.     }
  2359.       break;
  2360.  
  2361.     case CEIL_DIV_EXPR:
  2362.     case CEIL_MOD_EXPR:
  2363.       if (! can_clobber_op0)
  2364.     {
  2365.       adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
  2366.                         compute_mode);
  2367.       /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
  2368.          which will screw up mem refs for autoincrements.  */
  2369.       op0 = force_reg (compute_mode, op0);
  2370.     }
  2371.       if (log < 0)
  2372.     {
  2373.       rtx label = 0;
  2374.       if (! unsignedp)
  2375.         {
  2376.           label = gen_label_rtx ();
  2377.           emit_cmp_insn (adjusted_op0, const0_rtx, LE, 
  2378.                  NULL_RTX, compute_mode, 0, 0);
  2379.           emit_jump_insn (gen_ble (label));
  2380.         }
  2381.       expand_inc (adjusted_op0, op1);
  2382.       expand_dec (adjusted_op0, const1_rtx);
  2383.       if (! unsignedp)
  2384.         emit_label (label);
  2385.     }
  2386.       else
  2387.     {
  2388.       adjusted_op0 = expand_binop (compute_mode, add_optab,
  2389.                        adjusted_op0, plus_constant (op1, -1),
  2390.                        NULL_RTX, 0, OPTAB_LIB_WIDEN);
  2391.     }
  2392.       mod_insn_no_good = 1;
  2393.       break;
  2394.  
  2395.     case ROUND_DIV_EXPR:
  2396.     case ROUND_MOD_EXPR:
  2397.       if (! can_clobber_op0)
  2398.     {
  2399.       adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
  2400.                         compute_mode);
  2401.       /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
  2402.          which will screw up mem refs for autoincrements.  */
  2403.       op0 = force_reg (compute_mode, op0);
  2404.     }
  2405.       if (log < 0)
  2406.     {
  2407.       op1 = expand_shift (RSHIFT_EXPR, compute_mode, op1,
  2408.                   integer_one_node, NULL_RTX, 0);
  2409.       if (! unsignedp)
  2410.         {
  2411.           if (BRANCH_COST >= 2)
  2412.         {
  2413.           /* Negate OP1 if OP0 < 0.  Do this by computing a temporary
  2414.              that has all bits equal to the sign bit and exclusive
  2415.              or-ing it with OP1.  */
  2416.           rtx temp = gen_reg_rtx (compute_mode);
  2417.           temp = copy_to_suggested_reg (adjusted_op0, temp, compute_mode);
  2418.           temp = expand_shift (RSHIFT_EXPR, compute_mode, temp,
  2419.                        build_int_2 (size - 1, 0),
  2420.                        NULL_RTX, 0);
  2421.           op1 = expand_binop (compute_mode, xor_optab, op1, temp, op1,
  2422.                       unsignedp, OPTAB_LIB_WIDEN);
  2423.         }
  2424.           else
  2425.         {
  2426.           rtx label = gen_label_rtx ();
  2427.           emit_cmp_insn (adjusted_op0, const0_rtx, GE, NULL_RTX,
  2428.                  compute_mode, 0, 0);
  2429.           emit_jump_insn (gen_bge (label));
  2430.           expand_unop (compute_mode, neg_optab, op1, op1, 0);
  2431.           emit_label (label);
  2432.         }
  2433.         }
  2434.       expand_inc (adjusted_op0, op1);
  2435.     }
  2436.       else
  2437.     {
  2438.       op1 = GEN_INT (((HOST_WIDE_INT) 1 << log) / 2);
  2439.       expand_inc (adjusted_op0, op1);
  2440.     }
  2441.       mod_insn_no_good = 1;
  2442.       break;
  2443.     }
  2444.  
  2445.   if (rem_flag && !mod_insn_no_good)
  2446.     {
  2447.       /* Try to produce the remainder directly */
  2448.       if (log >= 0)
  2449.     result = expand_binop (compute_mode, and_optab, adjusted_op0,
  2450.                    GEN_INT (((HOST_WIDE_INT) 1 << log) - 1),
  2451.                    target, 1, OPTAB_LIB_WIDEN);
  2452.       else
  2453.     {
  2454.       /* See if we can do remainder without a library call.  */
  2455.       result = sign_expand_binop (mode, umod_optab, smod_optab,
  2456.                       adjusted_op0, op1, target,
  2457.                       unsignedp, OPTAB_WIDEN);
  2458.       if (result == 0)
  2459.         {
  2460.           /* No luck there.  Can we do remainder and divide at once
  2461.          without a library call?  */
  2462.           result = gen_reg_rtx (compute_mode);
  2463.           if (! expand_twoval_binop (unsignedp
  2464.                      ? udivmod_optab : sdivmod_optab,
  2465.                      adjusted_op0, op1,
  2466.                      NULL_RTX, result, unsignedp))
  2467.         result = 0;
  2468.         }
  2469.     }
  2470.     }
  2471.  
  2472.   if (result)
  2473.     return gen_lowpart (mode, result);
  2474.  
  2475.   /* Produce the quotient.  */
  2476.   if (log >= 0)
  2477.     result = expand_shift (RSHIFT_EXPR, compute_mode, adjusted_op0,
  2478.                build_int_2 (log, 0), target, unsignedp);
  2479.   else if (rem_flag && !mod_insn_no_good)
  2480.     /* If producing quotient in order to subtract for remainder,
  2481.        and a remainder subroutine would be ok,
  2482.        don't use a divide subroutine.  */
  2483.     result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
  2484.                 adjusted_op0, op1, NULL_RTX, unsignedp,
  2485.                 OPTAB_WIDEN);
  2486.   else
  2487.     {
  2488.       /* Try a quotient insn, but not a library call.  */
  2489.       result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
  2490.                   adjusted_op0, op1,
  2491.                   rem_flag ? NULL_RTX : target,
  2492.                   unsignedp, OPTAB_WIDEN);
  2493.       if (result == 0)
  2494.     {
  2495.       /* No luck there.  Try a quotient-and-remainder insn,
  2496.          keeping the quotient alone.  */
  2497.       result = gen_reg_rtx (mode);
  2498.       if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  2499.                      adjusted_op0, op1,
  2500.                      result, NULL_RTX, unsignedp))
  2501.         result = 0;
  2502.     }
  2503.  
  2504.       /* If still no luck, use a library call.  */
  2505.       if (result == 0)
  2506.     result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
  2507.                     adjusted_op0, op1,
  2508.                     rem_flag ? NULL_RTX : target,
  2509.                     unsignedp, OPTAB_LIB_WIDEN);
  2510.     }
  2511.  
  2512.   /* If we really want the remainder, get it by subtraction.  */
  2513.   if (rem_flag)
  2514.     {
  2515.       if (result == 0)
  2516.     /* No divide instruction either.  Use library for remainder.  */
  2517.     result = sign_expand_binop (compute_mode, umod_optab, smod_optab,
  2518.                     op0, op1, target,
  2519.                     unsignedp, OPTAB_LIB_WIDEN);
  2520.       else
  2521.     {
  2522.       /* We divided.  Now finish doing X - Y * (X / Y).  */
  2523.       result = expand_mult (compute_mode, result, op1, target, unsignedp);
  2524.       if (! result) abort ();
  2525.       result = expand_binop (compute_mode, sub_optab, op0,
  2526.                  result, target, unsignedp, OPTAB_LIB_WIDEN);
  2527.     }
  2528.     }
  2529.  
  2530.   if (result == 0)
  2531.     abort ();
  2532.  
  2533.   return gen_lowpart (mode, result);
  2534. }
  2535.  
  2536. /* Return a tree node with data type TYPE, describing the value of X.
  2537.    Usually this is an RTL_EXPR, if there is no obvious better choice.
  2538.    X may be an expression, however we only support those expressions
  2539.    generated by loop.c.   */
  2540.  
  2541. tree
  2542. make_tree (type, x)
  2543.      tree type;
  2544.      rtx x;
  2545. {
  2546.   tree t;
  2547.  
  2548.   switch (GET_CODE (x))
  2549.     {
  2550.     case CONST_INT:
  2551.       t = build_int_2 (INTVAL (x),
  2552.                ! TREE_UNSIGNED (type) && INTVAL (x) >= 0 ? 0 : -1);
  2553.       TREE_TYPE (t) = type;
  2554.       return t;
  2555.  
  2556.     case CONST_DOUBLE:
  2557.       if (GET_MODE (x) == VOIDmode)
  2558.     {
  2559.       t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
  2560.       TREE_TYPE (t) = type;
  2561.     }
  2562.       else
  2563.     {
  2564.       REAL_VALUE_TYPE d;
  2565.  
  2566.       REAL_VALUE_FROM_CONST_DOUBLE (d, x);
  2567.       t = build_real (type, d);
  2568.     }
  2569.  
  2570.       return t;
  2571.       
  2572.     case PLUS:
  2573.       return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
  2574.               make_tree (type, XEXP (x, 1))));
  2575.                                
  2576.     case MINUS:
  2577.       return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
  2578.               make_tree (type, XEXP (x, 1))));
  2579.                                
  2580.     case NEG:
  2581.       return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
  2582.  
  2583.     case MULT:
  2584.       return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
  2585.               make_tree (type, XEXP (x, 1))));
  2586.                               
  2587.     case ASHIFT:
  2588.       return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
  2589.               make_tree (type, XEXP (x, 1))));
  2590.                               
  2591.     case LSHIFTRT:
  2592.       return fold (convert (type,
  2593.                 build (RSHIFT_EXPR, unsigned_type (type),
  2594.                    make_tree (unsigned_type (type),
  2595.                           XEXP (x, 0)),
  2596.                    make_tree (type, XEXP (x, 1)))));
  2597.                               
  2598.     case ASHIFTRT:
  2599.       return fold (convert (type,
  2600.                 build (RSHIFT_EXPR, signed_type (type),
  2601.                    make_tree (signed_type (type), XEXP (x, 0)),
  2602.                    make_tree (type, XEXP (x, 1)))));
  2603.                               
  2604.     case DIV:
  2605.       if (TREE_CODE (type) != REAL_TYPE)
  2606.     t = signed_type (type);
  2607.       else
  2608.     t = type;
  2609.  
  2610.       return fold (convert (type,
  2611.                 build (TRUNC_DIV_EXPR, t,
  2612.                    make_tree (t, XEXP (x, 0)),
  2613.                    make_tree (t, XEXP (x, 1)))));
  2614.     case UDIV:
  2615.       t = unsigned_type (type);
  2616.       return fold (convert (type,
  2617.                 build (TRUNC_DIV_EXPR, t,
  2618.                    make_tree (t, XEXP (x, 0)),
  2619.                    make_tree (t, XEXP (x, 1)))));
  2620.    default:
  2621.       t = make_node (RTL_EXPR);
  2622.       TREE_TYPE (t) = type;
  2623.       RTL_EXPR_RTL (t) = x;
  2624.       /* There are no insns to be output
  2625.      when this rtl_expr is used.  */
  2626.       RTL_EXPR_SEQUENCE (t) = 0;
  2627.       return t;
  2628.     }
  2629. }
  2630.  
  2631. /* Return an rtx representing the value of X * MULT + ADD.
  2632.    TARGET is a suggestion for where to store the result (an rtx).
  2633.    MODE is the machine mode for the computation.
  2634.    X and MULT must have mode MODE.  ADD may have a different mode.
  2635.    So can X (defaults to same as MODE).
  2636.    UNSIGNEDP is non-zero to do unsigned multiplication.
  2637.    This may emit insns.  */
  2638.  
  2639. rtx
  2640. expand_mult_add (x, target, mult, add, mode, unsignedp)
  2641.      rtx x, target, mult, add;
  2642.      enum machine_mode mode;
  2643.      int unsignedp;
  2644. {
  2645.   tree type = type_for_mode (mode, unsignedp);
  2646.   tree add_type = (GET_MODE (add) == VOIDmode
  2647.            ? type : type_for_mode (GET_MODE (add), unsignedp));
  2648.   tree result =  fold (build (PLUS_EXPR, type,
  2649.                   fold (build (MULT_EXPR, type,
  2650.                        make_tree (type, x),
  2651.                        make_tree (type, mult))),
  2652.                   make_tree (add_type, add)));
  2653.  
  2654.   return expand_expr (result, target, VOIDmode, 0);
  2655. }
  2656.  
  2657. /* Compute the logical-and of OP0 and OP1, storing it in TARGET
  2658.    and returning TARGET.
  2659.  
  2660.    If TARGET is 0, a pseudo-register or constant is returned.  */
  2661.  
  2662. rtx
  2663. expand_and (op0, op1, target)
  2664.      rtx op0, op1, target;
  2665. {
  2666.   enum machine_mode mode = VOIDmode;
  2667.   rtx tem;
  2668.  
  2669.   if (GET_MODE (op0) != VOIDmode)
  2670.     mode = GET_MODE (op0);
  2671.   else if (GET_MODE (op1) != VOIDmode)
  2672.     mode = GET_MODE (op1);
  2673.  
  2674.   if (mode != VOIDmode)
  2675.     tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
  2676.   else if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == CONST_INT)
  2677.     tem = GEN_INT (INTVAL (op0) & INTVAL (op1));
  2678.   else
  2679.     abort ();
  2680.  
  2681.   if (target == 0)
  2682.     target = tem;
  2683.   else if (tem != target)
  2684.     emit_move_insn (target, tem);
  2685.   return target;
  2686. }
  2687.  
  2688. /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
  2689.    and storing in TARGET.  Normally return TARGET.
  2690.    Return 0 if that cannot be done.
  2691.  
  2692.    MODE is the mode to use for OP0 and OP1 should they be CONST_INTs.  If
  2693.    it is VOIDmode, they cannot both be CONST_INT.  
  2694.  
  2695.    UNSIGNEDP is for the case where we have to widen the operands
  2696.    to perform the operation.  It says to use zero-extension.
  2697.  
  2698.    NORMALIZEP is 1 if we should convert the result to be either zero
  2699.    or one one.  Normalize is -1 if we should convert the result to be
  2700.    either zero or -1.  If NORMALIZEP is zero, the result will be left
  2701.    "raw" out of the scc insn.  */
  2702.  
  2703. rtx
  2704. emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
  2705.      rtx target;
  2706.      enum rtx_code code;
  2707.      rtx op0, op1;
  2708.      enum machine_mode mode;
  2709.      int unsignedp;
  2710.      int normalizep;
  2711. {
  2712.   rtx subtarget;
  2713.   enum insn_code icode;
  2714.   enum machine_mode compare_mode;
  2715.   enum machine_mode target_mode = GET_MODE (target);
  2716.   rtx tem;
  2717.   rtx last = 0;
  2718.   rtx pattern, comparison;
  2719.  
  2720.   if (mode == VOIDmode)
  2721.     mode = GET_MODE (op0);
  2722.  
  2723.   /* For some comparisons with 1 and -1, we can convert this to 
  2724.      comparisons with zero.  This will often produce more opportunities for
  2725.      store-flag insns. */
  2726.  
  2727.   switch (code)
  2728.     {
  2729.     case LT:
  2730.       if (op1 == const1_rtx)
  2731.     op1 = const0_rtx, code = LE;
  2732.       break;
  2733.     case LE:
  2734.       if (op1 == constm1_rtx)
  2735.     op1 = const0_rtx, code = LT;
  2736.       break;
  2737.     case GE:
  2738.       if (op1 == const1_rtx)
  2739.     op1 = const0_rtx, code = GT;
  2740.       break;
  2741.     case GT:
  2742.       if (op1 == constm1_rtx)
  2743.     op1 = const0_rtx, code = GE;
  2744.       break;
  2745.     case GEU:
  2746.       if (op1 == const1_rtx)
  2747.     op1 = const0_rtx, code = NE;
  2748.       break;
  2749.     case LTU:
  2750.       if (op1 == const1_rtx)
  2751.     op1 = const0_rtx, code = EQ;
  2752.       break;
  2753.     }
  2754.  
  2755.   /* From now on, we won't change CODE, so set ICODE now.  */
  2756.   icode = setcc_gen_code[(int) code];
  2757.  
  2758.   /* If this is A < 0 or A >= 0, we can do this by taking the ones
  2759.      complement of A (for GE) and shifting the sign bit to the low bit.  */
  2760.   if (op1 == const0_rtx && (code == LT || code == GE)
  2761.       && GET_MODE_CLASS (mode) == MODE_INT
  2762.       && (normalizep || STORE_FLAG_VALUE == 1
  2763.       || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  2764.           && (STORE_FLAG_VALUE 
  2765.           == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
  2766.     {
  2767.       rtx subtarget = target;
  2768.  
  2769.       /* If the result is to be wider than OP0, it is best to convert it
  2770.      first.  If it is to be narrower, it is *incorrect* to convert it
  2771.      first.  */
  2772.       if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
  2773.     {
  2774.       op0 = protect_from_queue (op0, 0);
  2775.       op0 = convert_to_mode (target_mode, op0, 0);
  2776.       mode = target_mode;
  2777.     }
  2778.  
  2779.       if (target_mode != mode)
  2780.     subtarget = 0;
  2781.  
  2782.       if (code == GE)
  2783.     op0 = expand_unop (mode, one_cmpl_optab, op0, subtarget, 0);
  2784.  
  2785.       if (normalizep || STORE_FLAG_VALUE == 1)
  2786.     /* If we are supposed to produce a 0/1 value, we want to do
  2787.        a logical shift from the sign bit to the low-order bit; for
  2788.        a -1/0 value, we do an arithmetic shift.  */
  2789.     op0 = expand_shift (RSHIFT_EXPR, mode, op0,
  2790.                 size_int (GET_MODE_BITSIZE (mode) - 1),
  2791.                 subtarget, normalizep != -1);
  2792.  
  2793.       if (mode != target_mode)
  2794.     op0 = convert_to_mode (target_mode, op0, 0);
  2795.  
  2796.       return op0;
  2797.     }
  2798.  
  2799.   if (icode != CODE_FOR_nothing)
  2800.     {
  2801.       /* We think we may be able to do this with a scc insn.  Emit the
  2802.      comparison and then the scc insn.
  2803.  
  2804.      compare_from_rtx may call emit_queue, which would be deleted below
  2805.      if the scc insn fails.  So call it ourselves before setting LAST.  */
  2806.  
  2807.       emit_queue ();
  2808.       last = get_last_insn ();
  2809.  
  2810.       comparison
  2811.     = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
  2812.       if (GET_CODE (comparison) == CONST_INT)
  2813.     return (comparison == const0_rtx ? const0_rtx
  2814.         : normalizep == 1 ? const1_rtx
  2815.         : normalizep == -1 ? constm1_rtx
  2816.         : const_true_rtx);
  2817.  
  2818.       /* Get a reference to the target in the proper mode for this insn.  */
  2819.       compare_mode = insn_operand_mode[(int) icode][0];
  2820.       subtarget = target;
  2821.       if (preserve_subexpressions_p ()
  2822.       || ! (*insn_operand_predicate[(int) icode][0]) (subtarget, compare_mode))
  2823.     subtarget = gen_reg_rtx (compare_mode);
  2824.  
  2825.       pattern = GEN_FCN (icode) (subtarget);
  2826.       if (pattern)
  2827.     {
  2828.       emit_insn (pattern);
  2829.  
  2830.       /* If we are converting to a wider mode, first convert to
  2831.          TARGET_MODE, then normalize.  This produces better combining
  2832.          opportunities on machines that have a SIGN_EXTRACT when we are
  2833.          testing a single bit.  This mostly benefits the 68k.
  2834.  
  2835.          If STORE_FLAG_VALUE does not have the sign bit set when
  2836.          interpreted in COMPARE_MODE, we can do this conversion as
  2837.          unsigned, which is usually more efficient.  */
  2838.       if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
  2839.         {
  2840.           convert_move (target, subtarget,
  2841.                 (GET_MODE_BITSIZE (compare_mode)
  2842.                  <= HOST_BITS_PER_WIDE_INT)
  2843.                 && 0 == (STORE_FLAG_VALUE
  2844.                      & ((HOST_WIDE_INT) 1
  2845.                     << (GET_MODE_BITSIZE (compare_mode) -1))));
  2846.           op0 = target;
  2847.           compare_mode = target_mode;
  2848.         }
  2849.       else
  2850.         op0 = subtarget;
  2851.  
  2852.       /* If we want to keep subexpressions around, don't reuse our
  2853.          last target.  */
  2854.  
  2855.       if (preserve_subexpressions_p ())
  2856.         subtarget = 0;
  2857.  
  2858.       /* Now normalize to the proper value in COMPARE_MODE.  Sometimes
  2859.          we don't have to do anything.  */
  2860.       if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
  2861.         ;
  2862.       else if (normalizep == - STORE_FLAG_VALUE)
  2863.         op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
  2864.  
  2865.       /* We don't want to use STORE_FLAG_VALUE < 0 below since this
  2866.          makes it hard to use a value of just the sign bit due to
  2867.          ANSI integer constant typing rules.  */
  2868.       else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
  2869.            && (STORE_FLAG_VALUE
  2870.                & ((HOST_WIDE_INT) 1
  2871.               << (GET_MODE_BITSIZE (compare_mode) - 1))))
  2872.         op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
  2873.                 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
  2874.                 subtarget, normalizep == 1);
  2875.       else if (STORE_FLAG_VALUE & 1)
  2876.         {
  2877.           op0 = expand_and (op0, const1_rtx, subtarget);
  2878.           if (normalizep == -1)
  2879.         op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
  2880.         }
  2881.       else
  2882.         abort ();
  2883.  
  2884.       /* If we were converting to a smaller mode, do the 
  2885.          conversion now.  */
  2886.       if (target_mode != compare_mode)
  2887.         {
  2888.           convert_move (target, op0);
  2889.           return target;
  2890.         }
  2891.       else
  2892.         return op0;
  2893.     }
  2894.     }
  2895.  
  2896.   if (last)
  2897.     delete_insns_since (last);
  2898.  
  2899.   subtarget = target_mode == mode ? target : 0;
  2900.  
  2901.   /* If we reached here, we can't do this with a scc insn.  However, there
  2902.      are some comparisons that can be done directly.  For example, if
  2903.      this is an equality comparison of integers, we can try to exclusive-or
  2904.      (or subtract) the two operands and use a recursive call to try the
  2905.      comparison with zero.  Don't do any of these cases if branches are
  2906.      very cheap.  */
  2907.  
  2908.   if (BRANCH_COST >= 0
  2909.       && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
  2910.       && op1 != const0_rtx)
  2911.     {
  2912.       tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
  2913.               OPTAB_WIDEN);
  2914.  
  2915.       if (tem == 0)
  2916.     tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
  2917.                 OPTAB_WIDEN);
  2918.       if (tem != 0)
  2919.     tem = emit_store_flag (target, code, tem, const0_rtx,
  2920.                    mode, unsignedp, normalizep);
  2921.       if (tem == 0)
  2922.     delete_insns_since (last);
  2923.       return tem;
  2924.     }
  2925.  
  2926.   /* Some other cases we can do are EQ, NE, LE, and GT comparisons with 
  2927.      the constant zero.  Reject all other comparisons at this point.  Only
  2928.      do LE and GT if branches are expensive since they are expensive on
  2929.      2-operand machines.  */
  2930.  
  2931.   if (BRANCH_COST == 0
  2932.       || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
  2933.       || (code != EQ && code != NE
  2934.       && (BRANCH_COST <= 1 || (code != LE && code != GT))))
  2935.     return 0;
  2936.  
  2937.   /* See what we need to return.  We can only return a 1, -1, or the
  2938.      sign bit.  */
  2939.  
  2940.   if (normalizep == 0)
  2941.     {
  2942.       if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  2943.     normalizep = STORE_FLAG_VALUE;
  2944.  
  2945.       else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  2946.            && (STORE_FLAG_VALUE
  2947.            == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
  2948.     ;
  2949.       else
  2950.     return 0;
  2951.     }
  2952.  
  2953.   /* Try to put the result of the comparison in the sign bit.  Assume we can't
  2954.      do the necessary operation below.  */
  2955.  
  2956.   tem = 0;
  2957.  
  2958.   /* To see if A <= 0, compute (A | (A - 1)).  A <= 0 iff that result has
  2959.      the sign bit set.  */
  2960.  
  2961.   if (code == LE)
  2962.     {
  2963.       /* This is destructive, so SUBTARGET can't be OP0.  */
  2964.       if (rtx_equal_p (subtarget, op0))
  2965.     subtarget = 0;
  2966.  
  2967.       tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
  2968.               OPTAB_WIDEN);
  2969.       if (tem)
  2970.     tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
  2971.                 OPTAB_WIDEN);
  2972.     }
  2973.  
  2974.   /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
  2975.      number of bits in the mode of OP0, minus one.  */
  2976.  
  2977.   if (code == GT)
  2978.     {
  2979.       if (rtx_equal_p (subtarget, op0))
  2980.     subtarget = 0;
  2981.  
  2982.       tem = expand_shift (RSHIFT_EXPR, mode, op0,
  2983.               size_int (GET_MODE_BITSIZE (mode) - 1),
  2984.               subtarget, 0);
  2985.       tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
  2986.               OPTAB_WIDEN);
  2987.     }
  2988.                     
  2989.   if (code == EQ || code == NE)
  2990.     {
  2991.       /* For EQ or NE, one way to do the comparison is to apply an operation
  2992.      that converts the operand into a positive number if it is non-zero
  2993.      or zero if it was originally zero.  Then, for EQ, we subtract 1 and
  2994.      for NE we negate.  This puts the result in the sign bit.  Then we
  2995.      normalize with a shift, if needed. 
  2996.  
  2997.      Two operations that can do the above actions are ABS and FFS, so try
  2998.      them.  If that doesn't work, and MODE is smaller than a full word,
  2999.      we can use zero-extension to the wider mode (an unsigned conversion)
  3000.      as the operation.  */
  3001.  
  3002.       if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
  3003.     tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
  3004.       else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
  3005.     tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
  3006.       else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
  3007.     {
  3008.       mode = word_mode;
  3009.       op0 = protect_from_queue (op0, 0);
  3010.       tem = convert_to_mode (mode, op0, 1);
  3011.     }
  3012.  
  3013.       if (tem != 0)
  3014.     {
  3015.       if (code == EQ)
  3016.         tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
  3017.                 0, OPTAB_WIDEN);
  3018.       else
  3019.         tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
  3020.     }
  3021.  
  3022.       /* If we couldn't do it that way, for NE we can "or" the two's complement
  3023.      of the value with itself.  For EQ, we take the one's complement of
  3024.      that "or", which is an extra insn, so we only handle EQ if branches
  3025.      are expensive.  */
  3026.  
  3027.       if (tem == 0 && (code == NE || BRANCH_COST > 1))
  3028.     {
  3029.       if (rtx_equal_p (subtarget, op0))
  3030.         subtarget = 0;
  3031.  
  3032.       tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
  3033.       tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
  3034.                   OPTAB_WIDEN);
  3035.  
  3036.       if (tem && code == EQ)
  3037.         tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
  3038.     }
  3039.     }
  3040.  
  3041.   if (tem && normalizep)
  3042.     tem = expand_shift (RSHIFT_EXPR, mode, tem,
  3043.             size_int (GET_MODE_BITSIZE (mode) - 1),
  3044.             tem, normalizep == 1);
  3045.  
  3046.   if (tem && GET_MODE (tem) != target_mode)
  3047.     {
  3048.       convert_move (target, tem, 0);
  3049.       tem = target;
  3050.     }
  3051.  
  3052.   if (tem == 0)
  3053.     delete_insns_since (last);
  3054.  
  3055.   return tem;
  3056. }
  3057.